Android declare-styleable自定义控件属性

来源:互联网 发布:万得股票mac版 编辑:程序博客网 时间:2024/06/06 20:51

今天本身是看看简书,看看巴士想来点新东西,但是忽然发现这个 declare-styleable没用过,赶紧补习补习

下面我就简单给大家介绍一下
declare-styleable:declare-styleable是给自定义控件添加自定义属性用的。

起初,在自定义控件的时候,会要求构造3个方法中的一个或多个,好比我自定义的控件PersonView

blic 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);  }  

其中的AttributeSet attrs一般都没给它配置和使用,所以不知道这个东西到底怎么用,后来查看源码发现,这个配置在默认情况下使用的是系统自己的默认配置,一旦你直接设定了它的属性,默认属性就会被你的赋值所替代。

我们就是利用改变这个attrs来设置的
看下源码的片段

这里写图片描述

这里可以很清楚的看到
这个就是系统在默认的资源文件R.styleable中去获取相关的配置。
如果appearance不为空,它就会去寻找获取相关属性

我们就是利用这一点

下面给出的例子是我在网上借鉴的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>

可能这里有人会问,format是什么,里面的单词代表的又是什么意思。
format就是格式,里面的就是这个属性对应的格式,下面列出来大致的格式有:
1. reference:参考某一资源ID,以此类推
(1)属性定义:

<declare-styleable name = "名称"><attr name = "background" format = "reference" /></declare-styleable>

(2)属性使用:

<ImageViewandroid: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.设置好属性文件后,在使用的布局中写相关配置:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:personattr="http://schemas.android.com/apk/res/com.example.admin.declarestyleable"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <com.example.admin.declarestyleable.PersonView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        personattr:name="@string/person_name"        personattr:age="22"        personattr:sex="@string/person_sex"        personattr:weight ="fat"        personattr:adult ="false"        personattr:textSize="@dimen/text_size"/></RelativeLayout>

这里要先应用这个attr:

 xmlns:personattr="http://schemas.android.com/apk/res/com.example.admin.declarestyleable"

格式如下

xmlns:你自己定义的名称="http://schemas.android.com/apk/res/你程序的package包名"    (我这是com.example.admin.declarestyleable

这些必须设置的否则他是找不到你的declare-styleable设置的

最后给出view代码

package com.example.admin.declarestyleable;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.widget.TextView;import static com.example.admin.declarestyleable.R.attr.adult;/** * 描述:自定义一个View,继承了TextView,根据自定义属性去判断并输错个人信息 * @author RA * @blog http://blog.csdn.net/vipzjyno1 */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);        int age = tArray.getInt(R.styleable.PersonAttr_age, 15);        String sex=tArray.getString(R.styleable.PersonAttr_sex);        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"+"性别"+sex+"\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;    }}

注意这里会爆红不用管它
floattextSize=tArray.getDimension(R.styleable.PersonAttr_textSize,R.dimen.default_text_size);

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

0 0