Android 深入理解Android中的自訂屬性,深入理解android
轉載請標明出處:
http://blog.csdn.net/lmj623565791/article/details/45022631;
本文出自:【張鴻洋的部落格】
1、引言
對於自訂屬性,大家肯定都不陌生,遵循以下幾步,就可以實現:
ps:如果你對上述幾個步驟不熟悉,建議先熟悉下,再繼續~
那麼,我有幾個問題:
- 以上步驟是如何奏效的?
- styleable 的含義是什嗎?可以不寫嘛?我自訂屬性,我聲明屬性就好了,為什麼一定要寫個styleable呢?
- 如果系統中已經有了語義比較明確的屬性,我可以直接使用嘛?
- 構造方法中的有個參數叫做
AttributeSet
(eg: MyTextView(Context context, AttributeSet attrs) )這個參數看名字就知道包含的是參數的數組,那麼我能不能通過它去擷取我的自訂屬性呢?
- TypedArray是什麼鬼?從哪冒出來的,就要我去使用?
恩,針對這幾個問題,大家可以考慮下,如何回答呢?還是說:老子會背上述4個步驟就夠了~~
2、常見的例子
接下來通過例子來回答上述問題,問題的回答順序不定~~大家先看一個常見的例子,即上述幾個步驟的代碼化。
<?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>
package com.example.test;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.util.Log;import android.view.View;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); String text = ta.getString(R.styleable.test_testAttr); int textAttr = ta.getInteger(R.styleable.test_text, -1); Log.e(TAG, "text = " + text + " , textAttr = " + textAttr); ta.recycle(); }}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:zhy="http://schemas.android.com/apk/res/com.example.test" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.test.MyTextView android:layout_width="100dp" android:layout_height="200dp" zhy:testAttr="520" zhy:text="helloworld" /></RelativeLayout>
ok,大家花3s掃一下,運行結果為:
MyTextView: text = helloworld , textAttr = 520
應該都不意外吧,注意下,我的styleable的name寫的是test,所以說這裡並不要求一定是自訂View的名字。
3、AttributeSet與TypedArray
下面考慮:
構造方法中的有個參數叫做AttributeSet(eg: MyTextView(Context context, AttributeSet attrs) )這個參數看名字就知道包含的是參數的集合,那麼我能不能通過它去擷取我的自訂屬性呢?
首先AttributeSet中的確儲存的是該View聲明的所有的屬性,並且外面的確可以通過它去擷取(自訂的)屬性,怎麼做呢?
其實看下AttributeSet的方法就明白了,下面看代碼。
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 ... }
輸出:
MyTextView(4136): attrName = layout_width , attrVal = 100.0dipMyTextView(4136): attrName = layout_height , attrVal = 200.0dipMyTextView(4136): attrName = text , attrVal = helloworldMyTextView(4136): attrName = testAttr , attrVal = 520
結合上面的布局檔案,你發現了什嗎?
我擦,果然很神奇,真的獲得所有的屬性,恩,沒錯,通過AttributeSet可以獲得布局檔案中定義的所有屬性的key和value(還有一些方法,自己去嘗試),那麼是不是說TypedArray這個鬼可以拋棄了呢?答案是:NO!。
現在關注下一個問題:
TypedArray是什麼鬼?從哪冒出來的,就要我去使用?
我們簡單修改下,布局檔案中的MyTextView的屬性。
<com.example.test.MyTextView android:layout_width="@dimen/dp100" android:layout_height="@dimen/dp200" zhy:testAttr="520" zhy:text="@string/hello_world" />
現在再次啟動並執行結果是:
MyTextView(4692): attrName = layout_width , attrVal = @2131165234MyTextView(4692): attrName = layout_height , attrVal = @2131165235MyTextView(4692): attrName = text , attrVal = @2131361809MyTextView(4692): attrName = testAttr , attrVal = 520>>use typedarrayMyTextView(4692): text = Hello world! , textAttr = 520
發現了什嗎?通過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存在的意義,你就可以告訴他了。
4、declare-styleable
我們已經解決了兩個問題,接下來,我們看看布局檔案,我們有一個屬性叫做: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="testAttr" format="integer" /></resources>
喲西,so清爽~
* MyTextView實現
package com.example.test;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.util.Log;import android.view.View;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, 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 => mAttrR.styleable.test_text => ATTR_ANDROID_TEXT(0)R.styleable.test_testAttr => ATTR_TESTATTR(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所用的。
其實瞭解該原理是有用的,詳見:Android 自訂控制項 優雅實現元素間的分割線
ok,現在5個問題,回答了4個,第一個問題:
自訂屬性的幾個步驟是如何奏效的?
恩,上述以及基本涵蓋了這個問題的答案,大家自己總結,所以:略。
總結下今天的部落格。
- attrs.xml裡面的declare-styleable以及item,android會根據其在R.java中產生一些常量方便我們使用(aapt乾的),本質上,我們可以不聲明declare-styleable僅僅聲明所需的屬性即可。
- 我們在View的構造方法中,可以通過
AttributeSet去獲得自訂屬性的值,但是比較麻煩,而TypedArray可以很方便的便於我們去擷取。
- 我們在自訂View的時候,可以使用系統已經定義的屬性。
近期的更新計劃:自訂View的一些細節相關的Blog(重點會在互動上),Android最佳實務相關的文章,framework相關的一些文章,敬請期待。
群號:423372824
公眾號:hongyangAndroid
(歡迎關注,第一時間推送博文資訊)