declare-styleable的使用

简介:

declare-styleable:declare-styleable是给自定义控件添加自定义属性用的。

1.首先,先写attrs.xml

在res-vlaues文件夹下创建资源文件attrs.xml或则自定义一个资源文件xx.xml,都可以。

之后在里面配置declare-styleable ,name为PersonAttr

 

[html]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="PersonAttr">  
  4.         <attr name="name" format="reference" />  
  5.         <attr name="sex" format="reference" />  
  6.         <attr name="age" format="integer" />  
  7.         <attr name="weight">  
  8.             <flag name="fat" value="2" />  
  9.             <flag name="mid" value="1" />  
  10.             <flag name="thin" value="0" />  
  11.         </attr>  
  12.         <attr name="adult" format="boolean" />  
  13.         <attr name="textSize" format="dimension" />  
  14.     </declare-styleable>  
  15. </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",就是里面对应了自己的属性值。

11. reference|color: 颜色的资源文件。

12.reference|boolean:布尔值的资源文件

注意://由于reference是从资源文件中获取:所以在XML文件中写这个属性的时候必须 personattr:name="@string/app_name"这种格式,否则会出错

2.设置好属性文件后,在使用的布局中写相关配置:

 

[html]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:personattr="http://schemas.android.com/apk/res/com.example.declare_styleable"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <com.example.declare_styleable.PersonView  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         personattr:name="@string/person_name"   
  10.         personattr:weight ="fat"  
  11.         personattr:adult ="false"  
  12.         personattr:textSize="@dimen/text_size"/>  
  13.   
  14. </RelativeLayout>  


这里要先应用这个attr:

[html]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. xmlns:personattr="http://schemas.android.com/apk/res/com.example.declare_styleable"  

 

对应结构是:

 

[java]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. xmlns:你自己定义的名称="http://schemas.android.com/apk/res/你程序的package包名"    (我这是com.example.declare_styleable)  

 

 

包名是配置文件中   package="com.example.declare_styleable" 这样格式的

之后在布局中自定义的类中设相关属性:

你自己定义的名称:你设的属性 ="属性值";

 

3.最后在自定义控件的构造方法中获取你配置的属性值:

 

[java]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. public class PersonView extends TextView {  
  2.     public PersonView(Context context) {  
  3.         super(context);  
  4.         // TODO Auto-generated constructor stub  
  5.     }  
  6.   
  7.     public PersonView(Context context, AttributeSet attrs, int defStyle) {  
  8.         super(context, attrs, defStyle);  
  9.         // TODO Auto-generated constructor stub  
  10.     }  
  11.   
  12.     public PersonView(Context context, AttributeSet attrs) {  
  13.         super(context, attrs);  
  14.         // TODO Auto-generated constructor stub  
  15.         TypedArray tArray = context.obtainStyledAttributes(attrs,R.styleable.PersonAttr);//获取配置属性  
  16.         String name = tArray.getString(R.styleable.PersonAttr_name);<span style="font-family: Arial, Helvetica, sans-serif;">//得到属性name</span>  
  17.         int age = tArray.getInt(R.styleable.PersonAttr_age, 15);  
  18.         Boolean adult = tArray.getBoolean(R.styleable.PersonAttr_adult, false);  
  19.         String str_adult = getAdultStatus(adult);  
  20.         int weight = tArray.getInt(R.styleable.PersonAttr_weight, 1);// 默认是中等身材,属性为:1  
  21.         String str_weight = getWeightStatus(weight);//获得肥胖属性  
  22.         float textSize = tArray.getDimension(R.styleable.PersonAttr_textSize,R.dimen.default_text_size);// 如果你设置为DP等单位,会做像素转换  
  23.         tArray.recycle();//回收资源  
  24. //      setTextSize(textSize);//设置字体大小  
  25.         setText("姓名:" + name + "\n" + "年龄:" + age + "\n" + "是否成年:" + str_adult  
  26.                 + "\n" + "体形:" + str_weight);//给自定义的控件赋值  
  27.     }  
  28.       
  29.     /** 根据传入的值判断是否成年 */  
  30.     public String getAdultStatus(Boolean adult ){  
  31.         String str_adult = "未成年";  
  32.         if (adult) {  
  33.             str_adult = "成年";  
  34.         }  
  35.         return str_adult;  
  36.     }  
  37.       
  38.     /** 根据传入的值判断肥胖状态 */  
  39.     public String getWeightStatus(int weight){  
  40.         String str_weight = "中等";  
  41.         switch (weight) {  
  42.         case 0:  
  43.             str_weight = "瘦";  
  44.             break;  
  45.         case 1:  
  46.             str_weight = "中等";  
  47.             break;  
  48.         case 2:  
  49.             str_weight = "肥胖";  
  50.             break;  
  51.         default:  
  52.             break;  
  53.         }  
  54.         return str_weight;  
  55.     }  
  56. }  

运行后就是:

 



本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/5420932.html,如需转载请自行联系原作者

相关文章
|
8月前
|
存储 SQL 数据库
char、varchar、nvarchar、text的区别
char、varchar、nvarchar、text的区别
36 0
|
10月前
|
存储 索引
Char、Varchar和Nvarchar的区别
Char、Varchar和Nvarchar的区别
101 0
|
10月前
|
存储 SQL
Sql Server 中char、nchar、varchar、nvarchar这几种数据类型的区别
Sql Server 中char、nchar、varchar、nvarchar这几种数据类型的区别
144 0
|
11月前
|
存储 数据库 索引
char与varchar2的区别
char与varchar2的区别
|
存储 数据库 索引
六、char与varchar2的区别
六、char与varchar2的区别
90 0
|
存储
char、nchar、varchar、nvarchar 的区别
char、nchar、varchar、nvarchar 的区别
115 0
char、nchar、varchar、nvarchar 的区别
|
API
can't convert BigInt value to Number value
can't convert BigInt value to Number value
178 0
|
SQL 测试技术 Go
SELECT CAST(GETDATE() AS VARCHAR(10)) 显示不同格式的原因
原文:SELECT CAST(GETDATE() AS VARCHAR(10)) 显示不同格式的原因    开发人员测试时,发现生产服务器与测试服务器执行SELECT CAST(GETDATE() AS VARCHAR(10))语句显示的格式不一样。
1070 0