Android attr属性的定义/读取/出错/使用
2017-02-16 15:44
344 查看
有人认为它可以直接通过在代码类中进行set...(),然后去改变View中的字体大小,颜色等等属性。
那如果要直接在引用布局Layout对其进行设置属性该怎么办呢?
这就是本文重点要介绍的内容:自定义attr属性与读取
一、在res/values 文件下定义一个attrs.xml 文件.代码如下:
注意: format有以下几种类型及读取:
二、如果在attr中不同View引用相同属性名字时出现错误的解决方法
(1)引用代码(两个都引用了textColor会出错):
(2)错误提示:
三、enum/flag的特殊之处
(1)下面就列举flag(enum也类似),先看代码:
调用方法:
四、属性的定义与使用
(1)属性定义:
(2)属性使用:
这里推荐:
attr自定义标签详解文章,感觉这里对attr标签在布局中的用法写的蛮好
那如果要直接在引用布局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读取 |
1 | reference | 资源ID | attrs.getResourceId(R.styleable.View名称_attr名称, 默认值); |
2 | color | 颜色值 | attrs.getColor(R.styleable.View名称_attr名称, 默认值); |
3 | boolean | 布尔值 | attrs.getBoolean(R.styleable.View名称_attr名称, 默认值); |
4 | dimension | 尺寸值 | attrs.getDimension(R.styleable.View名称_attr名称, 默认值); |
5 | float | 浮点值 | attrs.getFloat(R.styleable.View名称_attr名称, 默认值); |
6 | integer | 整型值 | attrs.getInteger(R.styleable.View名称_attr名称, 默认值); |
7 | string | 字符串 | attrs.getString(R.styleable.View名称_attr名称); |
8 | fraction | 百分比(%) | attrs.getString(R.styleable.View名称_attr名称); |
9 | enum | 枚举值 | attrs.getInt(R.styleable.View名称_attr名称, 默认值); |
10 | flag | 位或运算 | attrs.getInt(R.styleable.View名称_attr名称, 默认值); |
(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标签在布局中的用法写的蛮好
相关文章推荐
- Android declare-styleable:自定义控件的属性(attr.xml,TypedArray)的使用
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
- Android declare-styleable:自定义控件的属性(attr.xml,TypedArray)的使用
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
- Android declare-styleable:自定义控件的属性(attr.xml,TypedArray)的使用
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用 .
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
- ANDROID 中自定义控件和属性(ATTR.XML,DECLARE-STYLEABLE,TYPEDARRAY)的方法和使用
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
- Android中使用shape来定义控件的显示属性
- Android declare-styleable:自定义控件的属性(attr.xml,TypedArray)的使用
- Android自定义View及自定义控件属性declare-styleable:自定义控件的属性(attr.xml,TypedArray)的使用
- [android]使用shape来定义控件的一些显示属性
- Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape的使用,对shape有了大体的了解,稍作总结: 先看下面的代码: <shape>