Android 自定义View并添加属性(转)
来源:互联网 发布:手机降调软件 编辑:程序博客网 时间:2024/05/29 17:55
http://blog.sina.com.cn/s/blog_5a6f39cf0101cfin.html
本文主要介绍如何为自定义的View添加属性以及属性的类型。代码示例定义见DropDownToRefreshListVie
1、添加自定义View的属性文件
在res/values中新建attrs.xml文件(文件名可另取,不过推荐用attrs.xml,可以将自定义属性都放入其中),内容为
<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="myViewDefinedAttr"> <attr name="attr1" format="boolean"/> <attr name="attr2" format="integer"/></declare-styleable> </resources>
定义名为myViewDefinedAttr的属性列表,这个name命名也可以用下划线形式。name会在下面第二步中使用。
<attr name="attr1"format="boolean"/>
2、自定义View中获取属性值
public class MyView extends View { private boolean attr1; private int attr2; public MyView(Context context){ super(context); } public MyView(Context context, AttributeSet attrs){ super(context, attrs); getAttrs(context, attrs); } public MyView(Context context, AttributeSet attrs, int defStyle){ super(context, attrs, defStyle); getAttrs(context, attrs); } private void getAttrs(Context context, AttributeSet attrs) { TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.myViewDefinedAttr); attr1 = ta.getBoolean(R.styleable.myViewDefinedAttr_attr1, true); attr2 = ta.getInt(R.styleable.myViewDefinedAttr_attr2, 0); ta.recycle(); }}
从上面可以看出,主要是通过context.obtainStyledAttributes得到属性及其值的对应列表。
ta.getBoolean(R.styleable.myViewDefinedAttr_attr1,true);表示得到属性名为attr1的boolean值,若不存在该属性,则默认为true。这里的R.styleable.myViewDefinedAttr_attr1为第一步中的属性列表名_属性名。
ta.recycle();为回收系统资源。
3、调用自定义View
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:myViewXmlns="http://schemas.android.com/apk/res/com.trinea.mypackage" android:layout_width="match_parent" android:layout_height="match_parent" > <com.trinea.android.common.view.MyView android:id="@+id/app_list" android:layout_width="match_parent" android:layout_height="match_parent" android:fastScrollEnabled="true" myViewXmlns:attr1="false" myViewXmlns:attr2="1" android:focusable="true" /> ……</RelativeLayout>
调用的主体为<com.trinea.android.common.view.MyView……/>中内容,需要注意的是跟上面代码类似在外面的布局文件中加入自己的命名空间再通过命名空间调用属性,即
xmlns:myViewXmlns="http://schemas.android.com/apk/res/com.trinea.mypackage",其中myViewXmlns为空间名可自取,值为http://schemas.android.com/apk/res/加当前应用的包名,即AndroidManifest.xml中manifest节点的package值,如下
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.trinea.mypackage"
4、自定义属性的类型
format表示的属性类型可以为boolean, string, integer, dimension, float,reference, color, fraction, enum, flag及其混合。
(1) boolean表示布尔值,调用如 xx:attr1="false"
(2)
(3) dimension表示尺寸值,调用如 xx:attr1="42dp"
(4) float表示浮点型,调用如 xx:attr1="0.7"
(5) color表示颜色值,调用如 xx:attr1="#00FF00"
(6) string表示字符串,调用如 xx:attr1="#adbddd"
(7) reference表示参考某一资源id,调用如 xx:attr1 = "@drawable/图片ID"
(8) fraction表示百分数,调用如 xx:attr1="30%"
以上类型定义都为<attr name="attr1"format="xxxtype"/>
(9) enum表示枚举值,定义为
<attr name="enum_attr"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /></attr>
调用如 xx:attr1="horizontal"
(10) flag表示位或运算,定义为
<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>
调用如:xx:attr1="stateUnspecified | stateUnchanged | stateHidden"
(11) 混合类型,定义为
<declare-styleable name = "combine_type"> <attr name = "background" format = "reference|color" /></declare-styleable>
调用如 xx:attr1 = "@drawable/图片ID|#DDFF00"
- Android 自定义View并添加属性(转)
- Android 自定义View并添加属性介绍
- Android 自定义View并添加属性
- Android 自定义View并添加属性
- Android 自定义View并添加属性
- Android 自定义View并添加属性
- Android自定义View 属性添加
- android 自定义view添加自定义xml属性
- 【Tech-Android-View】给自定义view添加自定义属性
- 【Tech-Android-View】给自定义view添加自定义属性
- android 自定义View研究(二) — 自定义控件添加属性
- 为android自定义View控件添加自定义的属性
- Android 自定义属性 view (继承View)
- (android)自定义View自定义属性
- 为自定义View添加属性
- 自定义Android View属性
- Android自定义 view属性
- android 自定义view属性
- HDU 1816
- hdoj.1248 寒冰王座 20140815
- ces
- Hibernate4.0之前,4.0--4.3.5以及4.3.5之后创建sessionFactory的方法改变比较大
- 用%c输入输出字符串
- Android 自定义View并添加属性(转)
- 数组和指针辨析
- 理解MySQL——索引与优化
- SSH面试题整理
- GRUB修复
- Color Length - UVa 1625 dp
- HDOJ 题目2255奔小康赚大钱(二分图,km匹配,模板)
- POJ3311 Hie with the Pie解题报告
- jquery mobile中的事件