【Android 自定义 View】之自定义属性相关

今天总结一下自定义 View 时自定义属性的相关。

定义

在 res/values 中创建 attrs.xml 文件。
示例代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="custom_color" format="color"/>

    <declare-styleable name="CustomAttribute">
        <attr name="custom_radius" format="dimension"/>
        <attr name="custom_color"/>
    </declare-styleable>
</resources>
  • attr 标签定义一个属性,name 是属性的名字,不能和其它属性的名字冲突,format 是属性的格式,可以用给一个属性指定多种格式;
  • declare-styleable 标签定义一个属性组,可以在里面定义属性,也可以直接引用 resource 标签下面定义的属性,区别就是不用写 format;

格式

自定义属性共有10种 format

  1. integer 整型值;
  2. float 浮点值;
  3. string 字符串;
  4. boolean 布尔值;
  5. dimension 尺寸值;
  6. color 颜色值;
  7. fraction 百分数;
  8. enum 枚举值;
  9. flag 位或运算
  10. reference 引用资源 ID

获取

属性的获取有两种方式:

获取 resource 下的 attr

第一种是直接获取 resource 标签下定义的 attr。
每一个 attr 都会在 R 文件的 attr 类中生成一个对应 ID,我们可以根据这个 ID 获取自己定义的属性,也可以获取系统定义的属性。
通常是在 View 的构造方法中获取:

    //获取自定义属性
    int[] customAttrs = {R.attr.custom_color};
    TypedArray a = context.obtainStyledAttributes(attrs, customAttrs);
    int color = a.getColor(0, Color.WHITE);
    a.recycle();
    //获取系统定义属性
    int[] customAttrs = {android.R.attr.color};
    TypedArray a = context.obtainStyledAttributes(attrs, customAttrs);
    mColor = a.getColor(0, mColor);
    a.recycle();

获取 declare-styleable 下的 attr

更常用的是获取 declare-styleable 属性集中的属性。
当定义 declare-styleable 时,R 文件在 styleable 内部类中自动生成一个 int[] 常量,数组中的元素是 declare-styleable 属性集中的属性的 ID,这样就不需要自己再定义 int[] 了,数组中的每一个元素也会生成一个 ID 指向数组中的元素,ID 格式为:属性集名_属性名,直接通过属性 ID 获取对应 attr。

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomAttribute);
    mColor = a.getColor(R.styleable.CustomAttribute_custom_color, mColor);
    mRadius = a.getDimension(R.styleable.CustomAttribute_custom_radius, mRadius);
    a.recycle();

也可以获取系统中定义的属性,这里 android:color 变成了 android_color

//把系统定义的属性放在属性集中
<declare-styleable name="CustomAttribute">
    <attr name="android:color"/>
</declare-styleable>
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomAttribute);
mColor = a.getColor(R.styleable.CustomAttribute_android_color, mColor);
a.recycle();

Context#obtainStyledAttributes

上面获取属性的方式是调用了 Context 类的 obtainStyledAttributes 方法,该方法有四个重载方法:

//从 Theme 中获取属性
obtainStyledAttributes(int[] attrs)
//从 style 中获取属性
obtainStyledAttributes(int resid, int[] attrs)
//从 layout 文件中获取属性
obtainStyledAttributes(AttributeSet set, int[] attrs)
//从 layout 文件中获取属性
obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr,int defStyleRes)

参数中的 int[] attrs 就是要获取的属性的名称,前面说过这个参数一般为 declare-styleable 在 R 文件的 styleable 类中生成的 ID。

obtainStyledAttributes(int[] attrs)

一个参数的方法是从应用的主题中获取属性,如果想从主题中获取我们自定义的属性,就需要在应用的主题中声明自定义的属性值。
加入定义了如下属性:

<declare-styleable name="CustomAttribute">
    <attr name="custom_color" format="color"/>
    <attr name="custom_radius" format="dimension"/>
</declare-styleable>

那么在应用的主题中:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--直接在主题中指定-->
    <item name="custom_color">#FF0000</item>
    <item name="custom_radius">100dp</item>
</style>
obtainStyledAttributes(int resid, int[] attrs)

该方法是从指定的 style 中获取,参数 resid 就是 style 在 R 文件中生成的 ID,我们需要在定义的 style 中声明相应的属性值:

<style name="CustomTheme">
    <item name="custom_color">#00FF00</item>
    <item name="custom_radius">10dp</item>
</style>
obtainStyledAttributes(AttributeSet set, int[] attrs)

该方法是最常用的,是从 layout 文件中获取,AttributeSet 类型的参数是从 xml 文件中解析出来的控件属性集合,也包括控件应用的 style 中声明的属性。

obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr,int defStyleRes)

第一个参数是从 xml 文件中解析出来的控件属性集合,第三个参数是 style.xml 文件中定义的某一 Theme 的 ID,第四个参数是 style.xml 中定义的某一 Style 的 ID。
该方法首先从第一个参数 AttributeSet 也就是 layout 文件中获取属性,如果没有取到属性,那么从第三个参数 defStyleAttr 指定的主题中获取,如果还没有取到,那么从第四个参数 defStyleRes 指定的 style 中获取,如果还没有获取到,那么返回的结果就为 null 了。

TypedArray

Context 类的 obtainStyledAttributes 方法实质调用的都是 Resources 类的内部类 Theme 类的对应的 obtainStyledAttributes 方法,最终调用的都是 ResourcesImpl.ThemeImpl 类的 obtainStyledAttributes 方法,这里不作讨论,该方法返回的是一个 TypedArray 对象,TypedArray 类就是一个获取到的属性值数组的容器,该类提供了一系列获取属性值的方法:

//获取属性的数量
public int getIndexCount()
//获取属性名
public int getIndex (int at)
//获取属性类型
public int getType (int index)
//在使用之后要进行回收才能重用
public void recycle ()

public boolean getBoolean (int index, boolean defValue)
public int getColor (int index, int defValue)
publicColorStateList getColorStateList(int index)
public float getDimension (int index, float defValue)
public int getDimensionPixelOffset (int index, int defValue)
public int getDimensionPixelSize (int index, int defValue)
publicDrawable getDrawable(int index)
public float getFloat (int index, float defValue)
public float getFraction (int index, int base, int pbase, float defValue)
public int getInt (int index, int defValue)
public int getInteger (int index, int defValue)
public intgetLayoutDimension(int index,Stringname)
public int getResourceId (int index, int defValue)
publicString (int index)
publicCharSequencegetText(int index)
publicCharSequence[]getTextArray(int index)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,219评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,363评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,933评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,020评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,400评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,640评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,896评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,597评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,327评论 1 244
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,581评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,072评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,399评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,054评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,849评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,672评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,585评论 2 270

推荐阅读更多精彩内容