Android attr属性的定义/读取/出错/使用

本文详细介绍了如何在Android中自定义View属性(attr),包括在res/values文件夹下的attrs.xml文件中定义不同类型的属性,如color、dimension等,并解决了在不同View中引用相同属性名时出现的错误。此外还介绍了enum/flag类型的特殊用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有人认为它可以直接通过在代码类中进行set...(),然后去改变View中的字体大小,颜色等等属性。

那如果要直接在引用布局Layout对其进行设置属性该怎么办呢?

这就是本文重点要介绍的内容:自定义attr属性与读取

一、在res/values 文件下定义一个attrs.xml 文件.代码如下:

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <declare-styleable name="View名称">  
        <attr name="textColor" format="color"/>  
        <attr name="textSize" format="dimension"/> 
    </declare-styleable>
</resources>  

注意: format有以下几种类型及读取:

序号format取值format说明format读取
1reference资源IDattrs.getResourceId(R.styleable.View名称_attr名称,  默认值);
2color颜色值attrs.getColor(R.styleable.View名称_attr名称, 默认值);
3boolean布尔值attrs.getBoolean(R.styleable.View名称_attr名称, 默认值);
4dimension尺寸值attrs.getDimension(R.styleable.View名称_attr名称, 默认值);
5float浮点值attrs.getFloat(R.styleable.View名称_attr名称, 默认值);
6integer整型值attrs.getInteger(R.styleable.View名称_attr名称, 默认值);
7string字符串attrs.getString(R.styleable.View名称_attr名称);
8fraction百分比(%)attrs.getString(R.styleable.View名称_attr名称);
9enum枚举值attrs.getInt(R.styleable.View名称_attr名称, 默认值);
10flag位或运算attrs.getInt(R.styleable.View名称_attr名称, 默认值);

二、如果在attr中不同View引用相同属性名字时出现错误的解决方法

(1)引用代码(两个都引用了textColor会出错):

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <declare-styleable name="View名称">  
        <attr name="textColor" format="color"/>  
        <attr name="textSize" format="dimension"/> 
    </declare-styleable>
    <declare-styleable name="View2名称">  
        <attr name="textColor" format="color"/>  
        <attr name="hint" format="reference" />
    </declare-styleable>
</resources>  

(2)错误提示:

Error:Execution failed for task ':包路径:mergeReleaseResources'.
> 本地包路径\src\main\res\values\attrs.xml: Error: Found item Attr/textColor more than one time
(3)解决方式:

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <attr name="textColor" format="color"/> 
    <declare-styleable name="View名称">  
        <attr name="textColor"/>  
        <attr name="textSize" format="dimension"/> 
    </declare-styleable>
    <declare-styleable name="View2名称">  
        <attr name="textColor"/>  
        <attr name="hint" format="reference" />
    </declare-styleable>
</resources>  

三、enum/flag的特殊之处

(1)下面就列举flag(enum也类似),先看代码:

<declare-styleable name = "View名称">
    <attr name="inputType">
        <flag name = "text" value = "0" />
        <flag name = "number" value = "1" />
        <flag name = "textPassword" value = "2" />
        <flag name = "numberPassword" value = "3" />
        <flag name = "numberDecimal" value = "4" />
    </attr>
</declare-styleable>
(2)这里是我在自定义一个EditText的时候,为了设置输入内容的类型:

调用方法:

setInputType(attrs.getInt(R.styleable.View名称_inputType, 0)); 

public void setInputType(int type) {
        switch (type) {
            case 0:
                mEditText.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
            case 1:
                mEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
                break;
            case 2:
                mEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                break;
            case 3:
                mEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                break;
            case 4:
                mEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                break;
        }
    }

四、属性的定义与使用

(1)属性定义:

<declare-styleable name = "名称">
    <attr name = "itemTextColor" format = "color" />
</declare-styleable>

(2)属性使用:

<TextView
	android:layout_width = "42dip"
	android:layout_height = "42dip"
	android:itemTextColor = "#00FF00"/>	

这里推荐: attr自定义标签详解文章,感觉这里对attr标签在布局中的用法写的蛮好


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值