您的位置:首页 > 移动开发 > Android开发

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

2017-02-16 15:44 344 查看
有人认为它可以直接通过在代码类中进行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标签在布局中的用法写的蛮好


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐