TypedArray的用法和declare-styleable:自定义控件的属性
TypedArray 用于存放
android 自定义控件
写在构造方法当中
// 把属性集 和我们自己定义的属性集合建立映射关系
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.setting_view_style);
String title = a.getString(R.styleable.setting_view_style_title);
checked_text = a.getString(R.styleable.setting_view_style_checked_text);
unchecked_text = a.getString(R.styleable.setting_view_style_unchecked_text);
// 获取styleable中的资源 format
tv_settingview_content.setText(unchecked_text);
tv_settingview_title.setText(title);
a.recycle();// 释放资源.
在res/value目录下 创建配置文件
attrs.xml
例1:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="setting_view_style">
<attr name="title" format="string"></attr>
<attr name="checked_text" format="string"></attr>
<attr name="unchecked_text" format="string"></attr>
</declare-styleable>
</resources>
例2:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TestAttr">
<attr name="name" format="reference" />
<attr name="age">
<flag name="child" value="10" />
<flag name="young" value="18" />
<flag name="oldman" value="60" />
</attr>
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
在XML布局中使用自定义布局时
需要 书写命名空间
包名是你所在的项目的根包.也就是在manifest里的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aaawqqq=http://schemas.android.com/apk/res/com.aaawqqq.ui
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
包名.类名
<com.aaawqqq.ui.SettingView
android:id="@+id/sv_task_setting_autokill"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
itheima:checked_text="已经开启"
itheima:title="自动清理内存"
itheima:unchecked_text="没有开启" / >
以下转自
http://blog.csdn.net/congqingbin/article/details/7869730
自定义属性数据类型简介:
一、reference:参考指定Theme中资源ID。
1.定义:
1
2
3
| <declare-styleable name="My"><attr name="label" format="reference" ></declare-styleable> |
2.使用:
1
| <Buttonzkx:label="@string/label" > |
二、Color:颜色
1.定义:
1
2
3
| <declare-styleable name="My"><attr name="textColor" format="color" /></declare-styleable> |
2.使用:
1
| <Button zkx:textColor="#ff0000"/> |
三、boolean:布尔值
1.定义:
1
2
3
| <declare-styleable name="My"><attr name="isVisible" format="boolean" /></declare-styleable> |
2.使用:
1
| <Button zkx:isVisible="false"/> |
四、dimension:尺寸值
1.定义:
1
2
3
| <declare-styleable name="My"><attr name="myWidth" format="dimension" /></declare-styleable> |
2.使用:
1
| <Button zkx:myWidth="100dip"/> |
五、float:浮点型
1.定义:
1
2
3
| <declare-styleable name="My"><attr name="fromAlpha" format="float" /></declare-styleable> |
2.使用:
1
| <alpha zkx:fromAlpha="0.3"/> |
六、integer:整型
1.定义:
1
2
3
| <declare-styleable name="My"><attr name="frameDuration" format="integer" /></declare-styleable> |
2.使用:
1
| <animated-rotate zkx:framesCount="22"/> |
七、string:字符串
1.定义:
1
2
3
| <declare-styleable name="My"><attr name="Name" format="string" /></declare-styleable> |
2.使用:
1
| <rotate zkx:pivotX="200%"/> |
八、fraction:百分数
1.定义:
1
2
3
| <declare-styleable name="My"><attr name="pivotX" format="fraction" /></declare-styleable> |
2.使用:
1
| <rotate zkx:Name="My name is zhang kun xiang"/> |
九、enum:枚举
1.定义:
1
2
3
4
5
| <declare-styleable name="My"><attr name="language"><enum name="English" value="1"/></attr></declare-styleable> |
2.使用:
1
| <Button zkx:language="English"/> |
十、flag:位或运算
1.定义:
1
2
3
4
5
6
| <declare-styleable name="My"><attr name="windowSoftInputMode"><flag name="stateUnspecified" value="1" /><flag name = "adjustNothing" value = "0x30" /></attr></declare-styleable> |
2.使用:
1
| <activity android:windowSoftInputMode="stateUnspecified | adjustNothing"> |
属性定义时可以指定多种类型值:
1
2
3
| <declare-styleable name = "名称"> <attr name="background" format="reference|color" /></declare-styleable> |
使用:
1
| <ImageView android:background = "@drawable/图片ID|#00FF00"/> |