declare-styleable:declare-styleable是給自定義控件添加自定義屬性用的。
1.首先,先寫attrs.xml
在res-vlaues文件夾下創建資源文件attrs.xml或則自定義一個資源文件xx.xml,都可以。
之后在里面配置declare-styleable ,name為PersonAttr
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="PersonAttr">
- <attr name="name" format="reference" />
- <attr name="sex" format="reference" />
- <attr name="age" format="integer" />
- <attr name="weight">
- <flag name="fat" value="2" />
- <flag name="mid" value="1" />
- <flag name="thin" value="0" />
- </attr>
- <attr name="adult" format="boolean" />
- <attr name="textSize" format="dimension" />
- </declare-styleable>
- </resources>
我這里設置了姓名name,性別sex,年齡age,以及特征屬性weight(fat,mid,thin內部的3個屬性及對應的屬性值),還有是否成年adult,和TextView的字體大小textView。
可能這里有人會問,format是什么,里面的單詞代表的又是什么意思。
format就是格式,里面的就是這個屬性對應的格式,下面列出來大致的格式有:
1. reference:參考某一資源ID,以此類推
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "background" format = "reference" />
</declare-styleable>
(2)屬性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/圖片ID"
/>
2. color:顏色值
<declare-styleable name = "名稱">
<attr name = "textColor" format = "color" />
</declare-styleable>
3. boolean:布爾值
<declare-styleable name = "名稱">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
4. dimension:尺寸值。注意,這里如果是dp那就會做像素轉換
<declare-styleable name = "名稱">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
5. float:浮點值。
6. integer:整型值。
7. string:字符串
8. fraction:百分數。
9. enum:枚舉值
10. flag:是自己定義的,類似於 android:gravity="top",就是里面對應了自己的屬性值。
12.reference|boolean:布爾值的資源文件
注意://由於reference是從資源文件中獲取:所以在XML文件中寫這個屬性的時候必須 personattr:name="@string/app_name"這種格式,否則會出錯
2.設置好屬性文件后,在使用的布局中寫相關配置:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:personattr="http://schemas.android.com/apk/res/com.example.declare_styleable"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <com.example.declare_styleable.PersonView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- personattr:name="@string/person_name"
- personattr:weight ="fat"
- personattr:adult ="false"
- personattr:textSize="@dimen/text_size"/>
- </RelativeLayout>
這里要先應用這個attr:
- xmlns:personattr="http://schemas.android.com/apk/res/com.example.declare_styleable"
對應結構是:
- xmlns:你自己定義的名稱="http://schemas.android.com/apk/res/你程序的package包名" (我這是com.example.declare_styleable)
包名是配置文件中 package="com.example.declare_styleable" 這樣格式的
之后在布局中自定義的類中設相關屬性:
你自己定義的名稱:你設的屬性 ="屬性值";
3.最后在自定義控件的構造方法中獲取你配置的屬性值:
- public class PersonView extends TextView {
- public PersonView(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- }
- public PersonView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- // TODO Auto-generated constructor stub
- }
- public PersonView(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- TypedArray tArray = context.obtainStyledAttributes(attrs,R.styleable.PersonAttr);//獲取配置屬性
- String name = tArray.getString(R.styleable.PersonAttr_name);<span style="font-family: Arial, Helvetica, sans-serif;">//得到屬性name</span>
- int age = tArray.getInt(R.styleable.PersonAttr_age, 15);
- Boolean adult = tArray.getBoolean(R.styleable.PersonAttr_adult, false);
- String str_adult = getAdultStatus(adult);
- int weight = tArray.getInt(R.styleable.PersonAttr_weight, 1);// 默認是中等身材,屬性為:1
- String str_weight = getWeightStatus(weight);//獲得肥胖屬性
- float textSize = tArray.getDimension(R.styleable.PersonAttr_textSize,R.dimen.default_text_size);// 如果你設置為DP等單位,會做像素轉換
- tArray.recycle();//回收資源
- // setTextSize(textSize);//設置字體大小
- setText("姓名:" + name + "\n" + "年齡:" + age + "\n" + "是否成年:" + str_adult
- + "\n" + "體形:" + str_weight);//給自定義的控件賦值
- }
- /** 根據傳入的值判斷是否成年 */
- public String getAdultStatus(Boolean adult ){
- String str_adult = "未成年";
- if (adult) {
- str_adult = "成年";
- }
- return str_adult;
- }
- /** 根據傳入的值判斷肥胖狀態 */
- public String getWeightStatus(int weight){
- String str_weight = "中等";
- switch (weight) {
- case 0:
- str_weight = "瘦";
- break;
- case 1:
- str_weight = "中等";
- break;
- case 2:
- str_weight = "肥胖";
- break;
- default:
- break;
- }
- return str_weight;
- }
- }
運行后就是: