今天自定义了一个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基础学习总结(16)——Java制作证书的工具keytool用法总结

    一.keytool的概念 keytool 是个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认证自己)或数据完整性以及认证服务.在 ...

  2. cocos2d-x 显示触摸操作(显示水波点击效果,用于视频演示)

    昨天刚刚參加玩游戏设计大赛, 积累了一些东西. 接下去将会逐个分享出来. 首先是显示触摸操作. 由于要演示我们的作品.使用试玩过程中, 假设没办法显示我们的触摸操作(像录制视频一样, 点击了屏幕某点, ...

  3. 通过C语言程序改动控制台的背景和前景颜色

    本文主要解说怎样通过C语言来改动dos背景和前景颜色.我们首先来看一下dos的背景颜色的属性. 打开開始菜单,点击执行,弹出执行对话框.输入cmd,回车. (打开dos控制台) 在命令提示符界面下,输 ...

  4. C++语言笔记系列之十——静态成员

    1.静态成员 (1)由keywordstatic修饰 静态变量定义语句在编译阶段就运行,运行过程中不再运行. (2)分类:静态数据成员.静态成员函数. (3)静态成员时类的全部对象共享的成员,而不是某 ...

  5. Android禁止ViewPager的左右滑动

    转载请注明出处:http://blog.csdn.net/allen315410/article/details/40744287 有时候在开发中会遇到一些"诡异"的要求,比方在V ...

  6. linux和unix的对照

    在之前的博客中说到,linux是一个单一内核的操作系统,但它与传统的单一内核UNIX操作系统不同. 在普通单一内核系统中,全部内核代码都是被静态编译和链接的. 而在linux中,能够动态的装入和卸载内 ...

  7. Weka中数据挖掘与机器学习系列之Weka简介(二)

    不多说,直接上干货! Weka简介 Weka是怀卡托智能分析环境(Waikato Environment for Knowledge Analysis)的英文字首缩写,官方网址为:http://www ...

  8. Java反射异常处理之InvocationTargetException

    java.lang.reflect.InvocationTargetException处理办法可能是没有引commons-lang3-3.x.jar包

  9. css3透明度

    http://www.haorooms.com/post/css_common //透明度 div{ background: #00c2de; opacity: 0.9; filter: alpha( ...

  10. jquery常规选择器再学习_1123

    jquery选择器基本模拟css语法来获取元素: 1 常规选择器 id 常见的元素标签 class 2 进阶选择器 组合选择器 常规选择器多个组合在一起 通配符选择器 * ,通常用于局部环境下 后代选 ...