对于自定义属性,你们确定都不陌生,遵循如下几步,就能够实现:java
那么,我有几个问题:android
AttributeSet
接下来经过例子来回答上述问题,问题的回答顺序不定~~你们先看一个常见的例子,即上述几个步骤的代码化。数组
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="Test"> <attr name="text" format="string"/> <attr name="textAttr" format="integer"/> </declare-styleable> </resources>
package qu.com.handlerthread; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; /** * Created by quguangle on 2016/11/25. */ public class MyTextView extends View { private static final String TAG = MyTextView.class.getSimpleName(); public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.Test); String text = typedArray.getString(R.styleable.Test_text); int textAttr = typedArray.getInteger(R.styleable.Test_textAttr,-1); Log.e(TAG,"text:"+text+"-----"+"textAttr:"+textAttr); typedArray.recycle(); } public MyTextView(Context context) { super(context); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:qgl="http://schemas.android.com/apk/res-auto" android:id="@+id/mMyLinearLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <qu.com.handlerthread.MyTextView android:id="@+id/btnTest" android:layout_width="match_parent" android:layout_height="wrap_content" qgl:text="Hello World" qgl:textAttr="250" /> </LinearLayout>
运行结果后,打印记录为:布局
应该都不意外吧,注意下,个人styleable的name写的是test,因此说这里并不要求必定是自定义View的名字。spa
下面考虑:.net
构造方法中的有个参数叫作AttributeSet
(eg: MyTextView(Context context, AttributeSet attrs) )这个参数看名字就知道包含的是参数的集合,那么我能不能经过它去获取个人自定义属性呢?code
首先AttributeSet
中的确保存的是该View声明的全部的属性,而且外面的确能够经过它去获取(自定义的)属性,怎么作呢?
其实看下AttributeSet
的方法就明白了,下面看代码。orm
public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); int count = attrs.getAttributeCount(); for (int i = 0; i < count; i++) { String attrName = attrs.getAttributeName(i); String attrVal = attrs.getAttributeValue(i); Log.e(TAG, "attrName = " + attrName + " , attrVal = " + attrVal); } // ==>use typedarray ... }
打印结果:xml
结合上面的布局文件,你发现了什么?
我擦,果真很神奇,真的得到全部的属性,恩,没错,经过AttributeSet
能够得到布局文件中定义的全部属性的key和value(还有一些方法,本身去尝试),那么是否是说TypedArray这个鬼能够抛弃了呢?答案是:NO!
。utf-8
如今关注下一个问题:
TypedArray是什么鬼?从哪冒出来的,就要我去使用?
咱们简单修改下,布局文件中的MyTextView的属性。
<qu.com.handlerthread.MyTextView android:id="@+id/btnTest" android:layout_width="match_parent" android:layout_height="wrap_content" qgl:text="@string/testView" qgl:textAttr="250" /> </LinearLayout>
如今再次运行的结果是:
发现了什么?经过AttributeSet
获取的值,若是是引用都变成了@+数字的字符串。你说,这玩意你能看懂么?那么你看看最后一行使用TypedArray获取的值,是否是瞬间明白了什么。
TypedArray
实际上是用来简化咱们的工做的,好比上例,若是布局中的属性的值是引用类型(好比:@dimen/dp100),若是使用AttributeSet
去得到最终的像素值,那么须要第一步拿到id,第二步再去解析id。而TypedArray正是帮咱们简化了这个过程。
贴一下:若是经过AttributeSet
获取最终的像素值的过程:
int widthDimensionId = attrs.getAttributeResourceValue(0, -1); Log.e(TAG, "layout_width= "+getResources().getDimension(widthDimensionId));
ok,如今别人问你TypedArray存在的意义,你就能够告诉他了。
咱们已经解决了两个问题,接下来,咱们看看布局文件,咱们有一个属性叫作:zhy:text
。
总所周知,系统提供了一个属性叫作:Android:text
,那么我以为直接使用android:text
更nice,这样的话,考虑问题:
若是系统中已经有了语义比较明确的属性,我能够直接使用嘛?
答案是能够的,怎么作呢?
直接在attrs.xml中使用android:text
属性。
<declare-styleable name="test"> <attr name="android:text" /> <attr name="testAttr" format="integer" /> </declare-styleable>
注意,这里咱们是使用已经定义好的属性,不须要去添加format
属性(注意声明和使用的区别,差异就是有没有format)。
而后在类中这么获取:ta.getString(R.styleable.test_android_text)
;布局文件中直接android:text="@string/hello_world"
便可。
这里提一下,系统中定义的属性,其实和咱们自定义属性的方式相似,你能够在sdk/platforms/android-xx/data/res/values
该目录下看到系统中定义的属性。而后你能够在系统提供的View(eg:TextView)的构造方法中发现TypedArray获取属性的代码(本身去看一下)。
ok,接下来,我在想,既然declare-styleable
这个标签的name都能随便写,这么随意的话,那么考虑问题:
styleable 的含义是什么?能够不写嘛?我自定义属性,我声明属性就行了,为何必定要写个styleable呢?
其实的确是能够不写的,怎么作呢?
那么如今的attrs.xml为:
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="text" format="string"/> <attr name="textAttr" format="integer"/> </resources>
MyTextView实现
** * Created by quguangle on 2016/11/25. */ public class MyTextView extends View { private static final String TAG = MyTextView.class.getSimpleName(); private static final int[] mAttr = { R.attr.textAttr,R.attr.Mytext}; private static final int ATTR_ANDROID_TEXT = 0; private static final int ATTR_TESTATTR = 1; public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); // ==>use typedarray 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
进行获取。
能够看到,咱们本来的:
R.styleable.test => mAttr R.styleable.test_text => ATTR_ANDROID_TEXT(0) R.styleable.test_testAttr => ATTR_TESTATTR(1)
打印结果:
11-25 13:37:16.086 29383-29383/qu.com.handlerthread E/MyTextView: text = 250 , textAttr = -1
那么其实呢?android在其内部也会这么作,按照传统的写法,它会在 R.java生成以下代码:
public static final class attr { public static final int testAttr=0x7f0100a9; } public static final class styleable { public static final int test_android_text = 0; public static final int test_testAttr = 1; public static final int[] test = { 0x0101014f, 0x7f0100a9 }; }
ok,根据上述你应该发现了什么。styleale的出现系统能够为咱们完成不少常量(int[]数组,下标常量)等的编写,简化咱们的开发工做(想一想若是一堆属性,本身编写常量,你得写成什么样的代码)。那么你们确定还知道declare-styleable
的name属性,通常状况下写的都是咱们自定义View的类名。主要为了直观的表达,该declare-styleable
的属性,都是改View所用的。
ok,如今5个问题,回答了4个,第一个问题:
自定义属性的几个步骤是如何奏效的?
恩,上述以及基本涵盖了这个问题的答案,你们本身总结,因此:略。
总结下今天的博客。
AttributeSet
去得到自定义属性的值,可是比较麻烦,而TypedArray
能够很方便的便于咱们去获取。