attrs看字面意思就是一组属性的集合,那attrs有什么用呢,在自定义View的时候,一般会自定义一些属性,通过构造方法中AttributeSet参数的封装,让我们能够获取到为View配置的属性。(自定义属性)
1.定义对应的属性
在values文件夹下新建一个attar_custom.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 自定义控件的名称 -->
<declare-styleable name="StrokeTextView">
<!-- 自定义的属性名称 和对应的单位 -->
<attr name="outerColor"format="color|reference" />
<attr name="innnerColor" format="color|reference" />
</declare-styleable>
</resources>
format表示这个属性的值的类型,类型有以下几种:
1.reference:引用资源(参考某一资源ID)
2.string:字符串
3.Color:颜色
4.dimension:尺寸值
5.fraction:百分数
6.enum:枚举类型
7.flag:位或运算
8. fraction:百分数。
(1)属性定义:
[html] view plaincopyprint?
<declare-styleable name="RotateDrawable">
<attr format="fraction" name="pivotX" />
<attr format="fraction" name="pivotY" />
</declare-styleable>
(2)属性使用:
<rotate
android:pivotX="200%"
android:pivotY="300%" />
9. enum:枚举值。
(1)属性定义:
<declare-styleable name="名称">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
(2)属性使用:
<LinearLayout
android:orientation="vertical" >
</LinearLayout>
10. flag:位或运算。
(1)属性定义:
<declare-styleable name="名称">
<attr name="windowSoftInputMode">
<flag name="stateUnspecified" value="0" />
<flag name="stateUnchanged" value="1" />
<flag name="stateHidden" value="2" />
<flag name="stateAlwaysHidden" value="3" />
<flag name="stateVisible" value="4" />
<flag name="stateAlwaysVisible" value="5" />
<flag name="adjustUnspecified" value="0x00" />
<flag name="adjustResize" value="0x10" />
<flag name="adjustPan" value="0x20" />
<flag name="adjustNothing" value="0x30" />
</attr>
</declare-styleable>
(2)属性使用:
<activity
android:windowSoftInputMode="stateUnspecified
| stateUnchanged | stateHidden" >
</activity>
注意:属性定义时可以指定多种类型值:
(1)属性定义:
[html] view plaincopyprint?
<declare-styleable name="名称">
<attr format="reference|color" name="background" />
</declare-styleable>
(2)属性使用:
[html] view plaincopyprint?
<ImageView
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/图片ID|#00FF00" />
2.在自定义代码里写
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.StrokeTextView);
Int outerColor = a.getColor(R.styleable.StrokeTextView_outerColor,
Color.BLUE);
Int innnerColor= a.getColor(R.styleable.StrokeTextView_innnerColor,
Color.Green);
3.在XML中定义自定义属性
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
通常我们在布局文件中使用自定义属性的时候会这样写<com.example.demo.StrokeTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textSize="28sp"
app:outerColor="#000000" 调用自定义属性
app:innnerColor="#ffffff" 调用自定义属性
android:layout_centerInParent="true"/>
注意,自定义的XML属性必须给自定义的控件使用。
属性优先级: 在构造函数中
Attribute attrs:从xml中定义的参数
int defStyleAttr :主题中优先级最高的属性
int detStyleRes:优先级较低的内置于View的系统style
如果defStyleAttr有效(defStyleAttr不为0或者有定义defStyleAttr),则defStyleRes无效;如果defStyleAttr无效,则defStyleRes有效。defStyleAttr的优先级比defStyleRes高。
属性优先级(1>2>3>4):
1、直接在layout.xml文件中添加的属性
2、在style中添加的属性
3、defStyleAttr或defStyleRes(二者不会同时有效)
4、theme中添加的属性