今天自定义了一个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. Unity 获得Android Context上下文

    1.获取Context AndroidJavaObject context = new AndroidJavaClass ("com.unity3d.player.UnityPlayer&q ...

  2. 【Henu ACM Round#19 C】 Developing Skills

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 优先把不是10的倍数的变成10的倍数. (优先%10比较大的数字增加 如果k还有剩余. 剩下的数字都是10的倍数了. 那么先加哪一个 ...

  3. 炜煌E30 E31微型热敏打印机 STM32 串口驱动

    设置为汉字模式 十六进制 命令:1C    26 USART_SendData(USART2,0x1C); while(USART_GetFlagStatus(USART2,USART_FLAG_TC ...

  4. android内存释放处理

    不知道大家对android内存释放都做什么样的处理,本人接触android不久,近期开发小游戏的过程中,由于游戏界面组件较多.刚玩游戏的时候感觉还好,可是重复进入游戏界面玩几次之后,游戏就会卡顿,我瞬 ...

  5. Java中AtomicInteger的使用!!!

    今天在看Volley的源码的时候,看到里面使用了AtomicInteger这个类,曾经没用过,今天看了一下API学习了一下: 首先介绍一下这个类的用处,这个类主要是用来替换java中的自增和自减操作, ...

  6. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

    1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...

  7. 未能将基于用户的Visual C++项目设置保存到user文件错误的解决

    作者:朱金灿 来源:http://blog.csdn.net/clever101 最近遇见一个诡异错误,将Win7家庭版升级到Win7旗舰版.然后使用原来安装的VS2008开发,保存工程时总是出现未能 ...

  8. 使用sh库执行shell命令

    python中执行shell命令 之前执行shell命令多是通过os.system(shell命令)的方式来执行,比较麻烦. 了解到sh是一个比subprocess好的库,能够执行shell命令 1. ...

  9. Linux下java/bin目录下的命令集合

    Linux下JAVA命令(1.7.0_79) 命令 详解 参数列表 示例 重要程度 资料 appletviewer Java applet 浏览器.appletviewer 命令可在脱离万维网浏览器环 ...

  10. 【Django】模板系统

    目录 一.变量 二.过滤器 Filters 2. length 3. filesizeformat 4. slice 5. add 6. first.last 7. join 8. truncatec ...