今天自定义了一个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. 从头认识java-18.2 主要的线程机制(5)-守护线程与非守护线程

    这一章节我们来讨论一下守护线程与非守护线程. 1.什么是守护线程?什么是非守护线程? 非守护线程:Java虚拟机在它全部非守护线程已经离开后自己主动离开. 守护线程:守护线程则是用来服务用户线程的,假 ...

  2. Redis之创建

    redis配置文件信息 public sealed class RedisConfigInfo { /// <summary> /// 可写的Redis链接地址 /// format:ip ...

  3. java静态类、静态方法、静态代码块,静态变量及实例方法,实例变量初始化顺序及内存管理,机制

    1.当一个类被第一次使用时,它需要被类加载器加载,而加载过程涉及以下两点: (1)在加载一个类时,如果它的父类还未被加载,那么其父类必须先被加载: (2)当类加载到内存之后,按照在代码中的出现顺序执行 ...

  4. Spring 热点面试题:

    1.谈谈你对Springaop的理解? spring用代理类包裹切面,把他们织入到Spring管理的bean中.也就是说代理类伪装成目标类,它会截取对目标类中方法的调用,让调用者对目标类的调用都先变成 ...

  5. Floyd-Warshall 算法-- 最短路径(适合节点密集的图)

     由于此算法时间复杂度为O(V³).大多数情况下不如迪杰斯特拉算法的.迪杰斯特拉算法适合于节点疏散的图.  演示样例图例如以下:     Step 1 创建节点与边的最短路径结果表(直接可达关系).数 ...

  6. [Python] Problem with Default Arguments

    Default arguments are a helpful feature, but there is one situation where they can be surprisingly u ...

  7. 指尖上的电商---(10)SolrAdmin中加入多核

    在Solr中有的时候,我们并不仅仅是须要一种形式的索引文件.可能须要多种不同数据的索引文件.这时我们就能够在同一个Solr以下创建 多核. 比方,我们在solr以下想把产品信息和分类信息各存放一个索引 ...

  8. vim 技巧之用宏命令批量处理文件

    今天遇到了一种情况,就是我需要同时修改34个文件中的某些字符串的内容,如果一个个打开需改的话,那也太麻烦了.后来就想着能不能通过vim的宏命令来修改呢?现在就总结下关于宏在文件列表中的应用1.首先,我 ...

  9. Thinkphp的 is null 查询条件是什么,以及exp表达式如何使用

    Thinkphp的 is null 查询条件是什么,以及exp表达式如何使用 一.总结 一句话总结:$map['name'] = array('exp','is null'); 1.is null判断 ...

  10. js插件---datatables如何使用

    js插件---datatables如何使用 一.总结 一句话总结:a.引入css和js(不要忘记css):b.js代码启动插件(里面可以用参数控制各种功能) 1.dataTables如何显示控制行(比 ...