转载请注明出处:
http://blog.csdn.net/qq347198688/article/details/52665975
本文出自:【何嘉龙的博客】
1.AttributeSet与TypedArray
在Android自定义属性详细剖析(一)中,MyListView的构造方法中,我们可以看到一个AttributeSet,这个参数看名字就知道包含的是参数的集合,那么我能不能通过它去获取我的自定义属性呢?
首先AttributeSet中的确保存的是该View声明的所有的属性,并且外面的确可以通过它去获取(自定义的)属性,怎么做呢?
其实看下AttributeSet的方法就明白了,下面看代码。
package com.example.attrtest;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
/**
* Created by dragonhaw on 2016/9/26.
*/
public class MyTextView extends View {
private static final String TAG = MyTextView.class.getSimpleName();
public MyTextView(Context context) {
this(context, null);
}
public MyTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
int count = attrs.getAttributeCount();
for (int i = 0; i < count; i++) {
String name = attrs.getAttributeName(i);
String value = attrs.getAttributeValue(i);
Log.e(TAG, "name = " + name + ", value = " + value);
}
}
}
输出:
09-26 12:31:53.029 3726-3726/com.example.attrtest E/MyTextView: name = layout_width, value = 100.0dip
09-26 12:31:53.029 3726-3726/com.example.attrtest E/MyTextView: name = layout_height, value = 200.0dip
09-26 12:31:53.029 3726-3726/com.example.attrtest E/MyTextView: name = text, value = Hello World!
09-26 12:31:53.029 3726-3726/com.example.attrtest E/MyTextView: name = testAttr, value = 520
结合上面的布局文件,你发现了什么?
我擦,果然很神奇,真的获得所有的属性,恩,没错,通过AttributeSet可以获得布局文件中定义的所有属性的key和value(还有一些方法,自己去尝试),那么是不是说TypedArray这个鬼可以抛弃了呢?答案是:NO!。
现在关注下一个问题:
TypedArray是什么鬼?从哪冒出来的,就要我去使用?(因为TintTypedArray跟TypedArray一样,故只考虑TypedArray)
我们简单修改下,布局文件中的MyTextView的属性。
<com.example.test.MyTextView
android:layout_width="@dimen/dp100"
android:layout_height="@dimen/dp200"
app:testAttr="520"
app:text="@string/hello_world" />
现在再次运行的结果是:
09-26 12:36:01.504 7702-7702/com.example.attrtest E/MyTextView: name = layout_width, value = @2131165260
09-26 12:36:01.504 7702-7702/com.example.attrtest E/MyTextView: name = layout_height, value = @2131165261
09-26 12:36:01.504 7702-7702/com.example.attrtest E/MyTextView: name = text, value = Hello World!
09-26 12:36:01.504 7702-7702/com.example.attrtest E/MyTextView: name = testAttr, value = 520
>>use typedarray
MyTextView(4692): text = Hello world! , textAttr = 520
TypedArray其实是用来简化我们的工作的,比如上例,如果布局中的属性的值是引用类型(比如:@dimen/dp100),如果使用AttributeSet去获得最终的像素值,那么需要第一步拿到id,第二步再去解析id。而TypedArray正是帮我们简化了这个过程。
贴一下:如果通过AttributeSet获取最终的像素值的过程:
int widthDimensionId = attrs.getAttributeResourceValue(0, -1);
Log.e(TAG, "layout_width= "+getResources().getDimension(widthDimensionId));
ok,现在别人问你TypedArray存在的意义,你就可以告诉他了。
2.declare-styleable
styleable 的含义是什么?可以不写嘛?我自定义属性,我声明属性就好了,为什么一定要写个styleable呢?
其实的确是可以不写的,怎么做呢?
- 首先删除declare-styleable的标签
那么现在的attrs.xml为:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="text" format="string"/>
<attr name="testAttr" format="integer"/>
</resources>
- MyTextView实现
package com.example.attrtest;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
/**
* Created by dragonhaw on 2016/9/26.
*/
public class MyTextView extends View {
private static final String TAG = MyTextView.class.getSimpleName();
private static final int[] mAttr = { android.R.attr.text, R.attr.testAttr };
private static final int ATTR_ANDROID_TEXT = 0;
private static final int ATTR_TESTATTR = 1;
public MyTextView(Context context) {
this(context, null);
}
public MyTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.obtainStyledAttributes(attrs, mAttr);
String text = ta.getString(ATTR_ANDROID_TEXT);
int textAttr = ta.getInteger(ATTR_TESTATTR, -1);
//输出 text = Hello world! , textAttr = 520
Log.e(TAG, "text = " + text + " , textAttr = " + textAttr);
ta.recycle();
}
}
貌似多了些代码,可以看到我们声明了一个int数组,数组中的元素就是我们想要获取的attr的id。并且我们根据元素的在数组中的位置,定义了一些整形的常量代表其下标,然后通过TypedArray进行获取。
ok,根据上述你应该发现了什么。styleale的出现系统可以为我们完成很多常量(int[]数组,下标常量)等的编写,简化我们的开发工作(想想如果一堆属性,自己编写常量,你得写成什么样的代码)。那么大家肯定还知道declare-styleable的name属性,一般情况下写的都是我们自定义View的类名。主要为了直观的表达,该declare-styleable的属性,都是改View所用的。
最后总结下自定义属性的步骤吧。
- 自定义一个CustomView(extends View )类;
- 编写values/attrs.xml,在其中编写styleable和item等标签元素;
- 在布局文件中CustomView使用自定义的属性(注意namespace);
- 在CustomView的构造方法中通过TypedArray获取 。