今天自定义了一个button按钮,需要添加一个属性,具体步骤如下

1.新属性的信息设定:在values目录下添加attrs.xml文件,在里面添加属性信息

<?xml version="1.0" encoding="utf-8"?>
<resources> <declare-styleable name="ColorButton">
<attr name="textNumber" format="reference|string" />
</declare-styleable> </resources>

2.在xml中添加

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myattr="http://schemas.android.com/apk/res/com.android.calculator2"
android:id="@+id/simplePad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:divider="@drawable/horizontal_line"
android:orientation="vertical"
android:showDividers="middle" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="@drawable/vertical_line"
android:showDividers="middle" > <com.android.calculator2.ColorButton
android:id="@+id/cle"
style="@style/simple_button_style" /> <com.android.calculator2.ColorButton
android:id="@+id/del"
style="@style/simple_button_style" /> <com.android.calculator2.ColorButton
android:id="@+id/div"
style="@style/simple_button_style"
myattr:textNumber="@string/div" /> <com.android.calculator2.ColorButton
android:id="@+id/mul"
style="@style/simple_button_style"
android:contentDescription="@string/divDesc"
myattr:textNumber="@string/mul" />
</LinearLayout> </LinearLayout>

这里有一点要注意,就是必须添加一条语句xmlns:myattr="http://schemas.android.com/apk/res/com.android.calculator2"  格式为xmlns:自己起个名字=“//schemas.android.com/apk/res/自己应用的包名”这个名字你会发现他的用处,就是在引用这个资源的时候,添加了一个区域前缀,就像android:text 自定义的使用就是myattr:textNumber

3.在代码中的引用

代码如下

package com.android.calculator2;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button; class ColorButton extends Button implements OnClickListener {
int CLICK_FEEDBACK_COLOR;
static final int CLICK_FEEDBACK_INTERVAL = 10;
static final int CLICK_FEEDBACK_DURATION = 350; private String mText;
float mTextX;
float mTextY;
long mAnimStart;
OnClickListener mListener; public ColorButton(Context context, AttributeSet attrs) {
super(context, attrs);
Calculator calc = (Calculator) context;
mListener = calc.mListener;
setOnClickListener(this);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ColorButton);
int resourceId = a.getResourceId(R.styleable.ColorButton_textNumber, 0);
mText = resourceId < 0 ? "" : a.getString(R.styleable.ColorButton_textNumber);
a.recycle();
} public void onClick(View view) {
mListener.onClick(this);
} @Override
public void onSizeChanged(int w, int h, int oldW, int oldH) {
measureText();
}
private void measureText() {
Paint paint = getPaint();
mTextX = (getWidth() - paint.measureText(getText().toString())) / 2;
mTextY = (getHeight() - paint.ascent() - paint.descent()) / 2;
} @Override
protected void onTextChanged(CharSequence text, int start, int before, int after) {
measureText();
}
//添加图片背景
 public StateListDrawable setbg(TypedArray normal, TypedArray prossed, int drawableId) {
StateListDrawable bg = new StateListDrawable();
Drawable normal_state = normal.getDrawable(drawableId);
Drawable pressed_state = prossed.getDrawable(drawableId);
bg.addState(new int[] {
android.R.attr.state_pressed
}, pressed_state);
bg.addState(new int[] {
-android.R.attr.state_pressed
}, normal_state);
return bg;
} public String getTextNumber() {
return mText;
}
}

注意:int resourceId = a.getResourceId(R.styleable.ColorButton_textNumber, 0)的时候,必须是attrs 中declare-styleable的名字+下划线+属性名

对这个类的引用的地方如下

  TypedArray simpleButtons = res.obtainTypedArray(R.array.simple_buttons_id);
TypedArray advanceButtons = res.obtainTypedArray(R.array.advance_buttons_id); TypedArray imagesAdvanceNormal = res.obtainTypedArray(R.array.advance_drawable_normal);
TypedArray imagesAdvancePressed = res.obtainTypedArray(R.array.advance_drawable_pressed);
TypedArray imagesSimpleNormal = res.obtainTypedArray(R.array.simple_drawable_normal);
TypedArray imagesSimplePressed = res.obtainTypedArray(R.array.simple_drawable_pressed);
for (int i = 0; i < simpleButtons.length(); i++) {
final ColorButton button = (ColorButton) simplePage.findViewById(simpleButtons
.getResourceId(i, 0));
StateListDrawable drawable = button.setbg(imagesSimpleNormal,
imagesSimplePressed, i);
button.setBackgroundDrawable(drawable);
}
for (int i = 0; i < advanceButtons.length(); i++) {
final ColorButton button = (ColorButton) advancedPage.findViewById(
advanceButtons.getResourceId(i, 0));
StateListDrawable drawable = button.setbg(imagesAdvanceNormal,
imagesAdvancePressed, i);
button.setBackgroundDrawable(drawable);
}
imagesSimpleNormal.recycle();
imagesSimplePressed.recycle();
imagesAdvanceNormal.recycle();
imagesAdvancePressed.recycle();

补充:

对于format属性

如下

<!--reference:参考某一资源ID-->
<!--属性定义-->
<declare-styleable name="名称">
<attr format="reference" name="background" />
</declare-styleable>
<!--属性使用-->
<ImageView
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/图片ID" />
<!--color:颜色值-->
<!--属性定义-->
<declare-styleable name="名称">
<attr format="color" name="textColor" />
</declare-styleable>
<!--属性使用-->
<TextView
android:layout_width="42dip"
android:layout_height="42dip"
android:textColor="#00FF00" />
<!--boolean:布尔值-->
<!--属性定义-->
<declare-styleable name="名称">
<attr format="boolean" name="focusable" />
</declare-styleable>
<!--属性使用-->
<Button
android:layout_width="42dip"
android:layout_height="42dip"
android:focusable="true" />
<!--dimension:尺寸值-->
<!--属性定义-->
<declare-styleable name="名称">
<attr format="dimension" name="layout_width" />
</declare-styleable>
<!--属性使用-->
<Button
android:layout_width="42dip"
android:layout_height="42dip" />
<!--float:浮点值-->
<!--属性定义-->
<declare-styleable name="AlphaAnimation">
<attr format="float" name="fromAlpha" />
<attr format="float" name="toAlpha" />
</declare-styleable>
<!--属性使用-->
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.7" />
<!--integer:整型值-->
<!--属性定义-->
<declare-styleable name="AnimatedRotateDrawable">
<attr format="integer" name="frameDuration" />
<attr format="integer" name="framesCount" />
</declare-styleable>
<!--属性使用-->
<animated-rotate
android:frameDuration="100"
android:framesCount="12"
/>
<!--string:字符串-->
<!--属性定义-->
<declare-styleable name="MapView">
<attr format="string" name="apiKey" />
</declare-styleable>
<!--属性使用-->
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g" />
<!--fraction:百分数-->
<!--属性定义-->
<declare-styleable name="RotateDrawable">
<attr format="fraction" name="pivotX" />
<attr format="fraction" name="pivotY" />
</declare-styleable>
<!--属性使用--> <rotate
android:pivotX="200%"
android:pivotY="300%"
/>
<!--enum:枚举值-->
<!--属性定义-->
<declare-styleable name="名称">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
<!--属性使用--> <LinearLayout
android:orientation="vertical" >
</LinearLayout>
<!--flag:位或运算-->
<!--属性定义-->
<declare-styleable name="名称">
<attr name="windowSoftInputMode">
<flag name="stateUnspecified" value="0" />
<flag name="stateUnchanged" value="1" />
<flag name="stateHidden" value="2" />
<flag name="stateAlwaysHidden" value="3" />
<flag name="stateVisible" value="4" />
<flag name="stateAlwaysVisible" value="5" />
<flag name="adjustUnspecified" value="0x00" />
<flag name="adjustResize" value="0x10" />
<flag name="adjustPan" value="0x20" />
<flag name="adjustNothing" value="0x30" />
</attr>
</declare-styleable>
<!--属性使用-->
<activity
android:windowSoftInputMode="stateUnspecified | stateUnchanged | stateHidden" >
</activity>
<!--注意:属性定义时可以指定多种类型值--> <declare-styleable name="名称">
<attr format="reference|color" name="background" />
</declare-styleable> <ImageView
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/图片ID|#00FF00" />

Button- 自定义控件添加自定义属性的更多相关文章

  1. Android自定义控件之自定义属性

    前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...

  2. Jquery获取select option动态添加自定义属性值失效

    Jquery获取select option动态添加自定义属性值失效 2014/12/31 11:49:19 中国学网转载 编辑:李强 http://www.xue163.com/588880/3909 ...

  3. Unity3D NGUI 给button按钮添加单间事件

    Unity3D中, NGUI 给button按钮添加单间事件的方法很多,在这里只给推荐一种比较常用的方法. 推荐方法:使用UIListener. 1.给button组价添加上UIListener.选择 ...

  4. dedecms:织梦文章如何添加“自定义属性”标签(sql命令行工具)

    dede织梦如何添加“自定义属性”标签“症状” 1.进入后台——系统——SQL命令行工具——运行SQL命令行,添加arcatt表字段: insert into`dede_arcatt`(sortid, ...

  5. android自定义控件---添加表情

    android自定义控件---添加表情 一.定义layout文件,图片不提供了 <?xml version="1.0" encoding="utf-8"? ...

  6. Android自定义控件及自定义属性

    Android自定义控件及自定义属性 自定义控件 创建自定义控件 自定义一个类,继承View 继承View还是哪个类,取决于你要实现一个什么样的控件 如果你要实现的是一个线性布局的组合控件,就可以继承 ...

  7. Android开发技巧——自定义控件之自定义属性

    Android开发技巧--自定义控件之自定义属性 掌握自定义控件是很重要的,因为通过自定义控件,能够:解决UI问题,优化布局性能,简化布局代码. 上一篇讲了如何通过xml把几个控件组织起来,并继承某个 ...

  8. Web开发笔记 #08# Jackson组合多个对象的属性构成JSON(以及添加自定义属性)

    参考文档:https://github.com/FasterXML/jackson-databind 关于ObjectMapper的线程安全 截自官方文档: 组合多个对象的属性构成JSON(以及添加自 ...

  9. WPF自定义控件的自定义属性绑定后不更新问题

    原文:WPF自定义控件的自定义属性绑定后不更新问题 需要在绑定时设置属性变更触发 UpdateSourceTrigger=PropertyChanged 例如: <Border CornerRa ...

  10. JavaScript: 高级技巧: window 对象也可以添加自定义属性

    JavaScript: 高级技巧: window 对象也可以添加自定义属性 例如 window.ntName = 'a';例如 window.ntXw = top; 优点是, window 无须等加载 ...

随机推荐

  1. Mysql忘记rootpassword

    1,停止MYSQL服务,CMD打开DOS窗体.输入 net stop mysql 2,在CMD命令行窗体,进入MYSQL安装文件夹 比方E:\Program Files\MySQL\MySQL Ser ...

  2. 软件project总结

    软件project的文档完结了.在这里面学到了好多东西.也通过它分析了对机房收费系统进行了更加具体的分析.尽管早就明确了之间的联系,可是越温习越体会到逻辑的重要性和全心全意为人民服务的精神. 这些文档 ...

  3. CentOS 配置防火墙操作实例(启、停、开、闭port)

    CentOS 配置防火墙操作实例(启.停.开.闭port): 注:防火墙的基本操作命令: 查询防火墙状态: [root@localhost ~]# service   iptables status& ...

  4. 并查集树数据结构hdu1325

    我的解法就是去构造了一棵树 以数组的存储方式 数组的值存放节点的根. 排除空树 剩下的就是出现环和多根节点的情况 也就是排除森林和有一个节点多个入度的情况 排除森林就用到了并查集 也就是便利数组让其仅 ...

  5. python学习二,字符串常用操作

    字符串可以说是在日常开发中应用最广泛的了,现在来总结下有关python中有关字符串一些常用操作 首先我们声明一个字符串变量 str = "hello world" 下面我们来依次介 ...

  6. spring的事务如何配置

    spring的声明式事务配置: 1. <!-- 配置sessionFactory --> <bean id="sessionFactory" class=&quo ...

  7. HTML5多文件上传

    文章转载自:http://xiechengxiong.com/288.html 一个简单的HTML5多文件上传demo. 以前我们上传文件的时候,如果通过js上传,我们无法在本地直接预览图片,还得跑到 ...

  8. CCF模拟题 最优灌溉

    最优灌溉 时间限制: 1.0s 内存限制: 256.0MB   问题描述 雷雷承包了很多片麦田,为了灌溉这些麦田,雷雷在第一个麦田挖了一口很深的水井,所有的麦田都从这口井来引水灌溉. 为了灌溉,雷雷需 ...

  9. CCF模拟 I’m stuck!

    I’m stuck! 时间限制: 1.0s 内存限制: 256.0MB   问题描述 给定一个R行C列的地图,地图的每一个方格可能是'#', '+', '-', '|', '.', 'S', 'T'七 ...

  10. 研究一些复杂java开源软件代码的体会(转)

    原文地址:http://herman-liu76.iteye.com/blog/2349026     有时候看源代码是非常有趣的事情,象是思考游戏,象是思考棋局...     平时做J2EE项目中, ...