首先,自定义属性的四个步骤:
- 自定义一个CustomView(extends View )类
- 编写values/attrs.xml,在其中编写styleable和item等标签元素
- 在布局文件中CustomView使用自定义的属性(注意namespace)
- 在CustomView的构造方法中通过TypedArray获取
问题一:自定义属性在哪里设置?文件声明
在Values包的下面创建一个attr.xml的文件夹,在里面设置
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="test">
<attr name="text" format="string" />
<attr name="testAttr" format="integer" />
</declare-styleable>
</resources>
自定义属性:
public class MyTextView extends View {
private static final String TAG = MyTextView.class.getSimpleName();
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.test);
int text_attr = ta.getInteger(R.styleable.test_testAttr,-1);
String text = ta.getString(R.styleable.test_text);
Log.e(TAG, "text = " + text + " , textAttr = " + text_attr);
ta.recycle();
}
在布局文件中使用
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:ztd="http://schemas.android.com/apk/res-auto">
<cast.it.zhang.demo01.MyTextView
android:layout_width="100dp"
android:layout_height="200dp"
ztd:testAttr="520"
ztd:text="helloworld" />
问题:编写的的时候出现了这个异常,这个要慎用,一般不会出现这样的错误,除非是你的逻辑有问题,这样不会报错,但结果不会改变。
Caused by: java.lang.UnsupportedOperationException: Can’t convert to integer: type=0x3
经查发现在引用资源的时候出现了错误
int textAttr = ta.getInteger(R.styleable.test_text, -1);
解决方法:
在执行ta.getInteger()方法的时候出现报错,根据报错信息字面意思是因为获取的资源id无法转换为integer类型才报错的,于是我将getInteger()改为了getResourceId()方法,这回程序就运行正常了。
AttributeSet与TypedArray
1。通过AttributeSet可以获得布局文件中定义的所有属性的key和value
2.TypedArray其实是用来简化我们的工作的,比如上例,如果布局中的属性的值是引用类型(比如:@dimen/dp100),如果使用AttributeSet去获得最终的像素值,那么需要第一步拿到id,第二步再去解析id。而TypedArray正是帮我们简化了这个过程
attrs.xml里面的declare-styleable以及item,android会根据其在R.java中生成一些常量方便我们使用(aapt干的),本质上,我们可以不声明declare-styleable仅仅声明所需的属性即可。
我们在View的构造方法中,可以通过AttributeSet去获得自定义属性的值,但是比较麻烦,而TypedArray可以很方便的便于我们去获取。
我们在自定义View的时候,可以使用系统已经定义的属性。
近期的更新计划:自定义View的一些细节相关的Blog(重点会在交互上),Android最佳实践相关的文章,framework相关的一些文章,
那么你看看最后一行使用TypedArray获取的值,是不是瞬间明白了什么。
多个类定义attr属性重复的问题:Attribute “xxx” has already been defined
如果从单独开发app的话,可能不会遇到多个自定义类的attribute 的名字重复的问题。但是如果是团队合作开发的话,可能会碰到这样的问题,A和B自定义的两个类都用了同一个名字来定义属性,这时系统会报出警告,Attribute “xxx” has already been defined. A和B又都不想修改自己的名字,这时就很头痛。
下面举个例子,在values文件夹下定义一个上面的attrs.xml的文件,eclipse即会报错:Attribute “icon” has already been defined。因为在PreferenceHeader, Preference两个属性集里定义了两个相同的属性。
[html] view plain copy
<? xml version = "1.0" encoding = "utf-8" ?>
< resources >
< declare-styleable name= "PreferenceHeader" >
<!-- Identifier value for the header. -->
< attr name= "id" format = "integer"/>
< attr name= "icon" format = "integer" />
<!-- The fragment that is displayed when the user selects this item. -->
</declare-styleable >
< declare-styleable name= "Preference" >
< attr name= "icon" format = "integer" />
<!-- The key to store the Preference value. -->
< attr name= "key" format = "string" />
</declare-styleable >
</ resources >
解决方案:
1.在xml文件里前面先声明属性
2.然后在属性集合里引用声明的属性即可。
以上面的文件为例来修改,如下:
[html] view plain copy
<? xml version = "1.0" encoding = "utf-8" ?>
< resources >
< attr name = "icon" format = "integer" />
< declare-styleable name= "PreferenceHeader" >
<!-- Identifier value for the header. -->
< attr name= "id" format = "integer"/>
< attr name= "icon" />
<!-- The fragment that is displayed when the user selects this item. -->
</declare-styleable >
< declare-styleable name= "Preference" >
< attr name= "icon" />
<!-- The key to store the Preference value. -->
< attr name= "key" format = "string" />
</declare-styleable >