当我们自定义View时,有时候需要提供一些自定义的参数,怎样来提供这些自定义的属性呢?步骤如下:
1、在res/values下建立一个attrs.xml文件;
2、在自定义View构造函数里面获取这些属性;
3、在布局文件中指定命名空间以及填写属性。
新建attrs.xml文件
res/values/attrs.xml,使用到declare-styleable
标签,该标签的内容是attr
标签,有name
和format
两个属性。name顾名思义就是自定义属性的名字,而format则有string、color、boolean、dimension、integer、float、fraction等值。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="myViewText" format="string"></attr>
<attr name="isDigits" format="boolean"></attr>
</declare-styleable>
</resources>
构造函数获取该属性
通过TypeArray获取到自定义的属性,然后获取布局文件中的值。
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyView, defStyleAttr, 0);
String str = ta.getString(R.styleable.MyView_myViewText);
boolean isDigits = ta.getBoolean(R.styleable.MyView_isDigits, false);
ta.recycle();
Log.e("TAG", str + " " + isDigits);
}
当我们没有在使用TypedArray后调用recycle,编译器会提示“This TypedArray should be recycled after use with #recycle()”。官方的解释是:回收TypedArray,以便后面重用。
当我们不做是否有此属性的判断就获取一个为null的String对象,操作时就会有空指针异常。除了上面这种写法以外还有常见的另外一种,这种写法比上面的写法安全一点。
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyView, defStyleAttr, 0);
// 布局文件中使用自定义属性的个数
int count = ta.getIndexCount();
for (int i = 0; i < count; i++) {
int index = ta.getIndex(i);
// 判断出有该属性时才做操作
switch (index) {
case R.styleable.MyView_myViewText:
String str = ta.getString(index);
Log.e("TAG", count + " " + str);
break;
case R.styleable.MyView_isDigits:
boolean bool = ta.getBoolean(index, false);
Log.e("TAG", count + " " + bool);
break;
}
}
ta.recycle();
本例只是简单打印出属性值,真正的自定义View需要根据这些属性值影响绘制流程。
布局文件中使用
指定命名空间:
xmlns:custom="http://schemas.android.com/apk/res-auto"
注意:使用gradle构建一律使用上面的格式,而如果是Eclipse工程最后apk/则指定包名。
使用属性:
custom:isDigits="false"
custom:myViewText="hello world!" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.leelit.example.customview.MyView
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:isDigits="false"
custom:myViewText="hello world!" />
</RelativeLayout>