理解Android中的自定義屬性
本文實(shí)例講解了Android中的自定義屬性,具體內(nèi)容如下
1、引言
對(duì)于自定義屬性,大家肯定都不陌生,遵循以下幾步,就可以實(shí)現(xiàn):
- 自定義一個(gè)CustomView(extends View )類
- 編寫values/attrs.xml,在其中編寫styleable和item等標(biāo)簽元素
- 在布局文件中CustomView使用自定義的屬性(注意namespace)
- 在CustomView的構(gòu)造方法中通過TypedArray獲取
ps:如果你對(duì)上述幾個(gè)步驟不熟悉,建議先熟悉下,再繼續(xù)~
那么,我有幾個(gè)問題:
- 以上步驟是如何奏效的?
- styleable 的含義是什么?可以不寫嘛?我自定義屬性,我聲明屬性就好了,為什么一定要寫個(gè)styleable呢?
- 如果系統(tǒng)中已經(jīng)有了語義比較明確的屬性,我可以直接使用嘛?
- 構(gòu)造方法中的有個(gè)參數(shù)叫做AttributeSet
(eg: MyTextView(Context context, AttributeSet attrs) )這個(gè)參數(shù)看名字就知道包含的是參數(shù)的數(shù)組,那么我能不能通過它去獲取我的自定義屬性呢?
- TypedArray是什么鬼?從哪冒出來的,就要我去使用?
恩,針對(duì)這幾個(gè)問題,大家可以考慮下,如何回答呢?還是說:老子會(huì)背上述4個(gè)步驟就夠了~~
2、常見的例子
接下來通過例子來回答上述問題,問題的回答順序不定~~大家先看一個(gè)常見的例子,即上述幾個(gè)步驟的代碼化。
自定義屬性的聲明文件
<?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>
自定義View類
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掃一下,運(yùn)行結(jié)果為:
MyTextView: text = helloworld , textAttr = 520
應(yīng)該都不意外吧,注意下,我的styleable的name寫的是test,所以說這里并不要求一定是自定義View的名字。
3、AttributeSet與TypedArray
下面考慮:
構(gòu)造方法中的有個(gè)參數(shù)叫做AttributeSet(eg: MyTextView(Context context, AttributeSet attrs) )這個(gè)參數(shù)看名字就知道包含的是參數(shù)的集合,那么我能不能通過它去獲取我的自定義屬性呢?
首先AttributeSet中的確保存的是該View聲明的所有的屬性,并且外面的確可以通過它去獲?。ㄗ远x的)屬性,怎么做呢?
其實(shí)看下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.0dip MyTextView(4136): attrName = layout_height , attrVal = 200.0dip MyTextView(4136): attrName = text , attrVal = helloworld MyTextView(4136): attrName = testAttr , attrVal = 520
結(jié)合上面的布局文件,你發(fā)現(xiàn)了什么?
我擦,果然很神奇,真的獲得所有的屬性,恩,沒錯(cuò),通過AttributeSet可以獲得布局文件中定義的所有屬性的key和value(還有一些方法,自己去嘗試),那么是不是說TypedArray這個(gè)鬼可以拋棄了呢?答案是:NO!。
現(xiàn)在關(guān)注下一個(gè)問題:
TypedArray是什么鬼?從哪冒出來的,就要我去使用?
我們簡單修改下,布局文件中的MyTextView的屬性。
<com.example.test.MyTextView android:layout_width="@dimen/dp100" android:layout_height="@dimen/dp200" zhy:testAttr="520" zhy:text="@string/hello_world" />
現(xiàn)在再次運(yùn)行的結(jié)果是:
MyTextView(4692): attrName = layout_width , attrVal = @2131165234 MyTextView(4692): attrName = layout_height , attrVal = @2131165235 MyTextView(4692): attrName = text , attrVal = @2131361809 MyTextView(4692): attrName = testAttr , attrVal = 520 >>use typedarray MyTextView(4692): text = Hello world! , textAttr = 520
發(fā)現(xiàn)了什么?通過AttributeSet獲取的值,如果是引用都變成了@+數(shù)字的字符串。你說,這玩意你能看懂么?那么你看看最后一行使用TypedArray獲取的值,是不是瞬間明白了什么。
TypedArray其實(shí)是用來簡化我們的工作的,比如上例,如果布局中的屬性的值是引用類型(比如:@dimen/dp100),如果使用AttributeSet去獲得最終的像素值,那么需要第一步拿到id,第二步再去解析id。而TypedArray正是幫我們簡化了這個(gè)過程。
貼一下:如果通過AttributeSet獲取最終的像素值的過程:
int widthDimensionId = attrs.getAttributeResourceValue(0, -1); Log.e(TAG, "layout_width= "+getResources().getDimension(widthDimensionId));
ok,現(xiàn)在別人問你TypedArray存在的意義,你就可以告訴他了。
4、declare-styleable
我們已經(jīng)解決了兩個(gè)問題,接下來,我們看看布局文件,我們有一個(gè)屬性叫做:zhy:text。
總所周知,系統(tǒng)提供了一個(gè)屬性叫做:android:text,那么我覺得直接使用android:text更nice,這樣的話,考慮問題:
如果系統(tǒng)中已經(jīng)有了語義比較明確的屬性,我可以直接使用嘛?
答案是可以的,怎么做呢?
直接在attrs.xml中使用android:text屬性。
<declare-styleable name="test"> <attr name="android:text" /> <attr name="testAttr" format="integer" /> </declare-styleable>
注意,這里我們是使用已經(jīng)定義好的屬性,不需要去添加format屬性(注意聲明和使用的區(qū)別,差別就是有沒有format)。
然后在類中這么獲?。簍a.getString(R.styleable.test_android_text);布局文件中直接android:text="@string/hello_world"即可。
這里提一下,系統(tǒng)中定義的屬性,其實(shí)和我們自定義屬性的方式類似,你可以在sdk/platforms/android-xx/data/res/values該目錄下看到系統(tǒng)中定義的屬性。然后你可以在系統(tǒng)提供的View(eg:TextView)的構(gòu)造方法中發(fā)現(xiàn)TypedArray獲取屬性的代碼(自己去看一下)。
ok,接下來,我在想,既然declare-styleable這個(gè)標(biāo)簽的name都能隨便寫,這么隨意的話,那么考慮問題:
styleable 的含義是什么?可以不寫嘛?我自定義屬性,我聲明屬性就好了,為什么一定要寫個(gè)styleable呢?
其實(shí)的確是可以不寫的,怎么做呢?
首先刪除declare-styleable的標(biāo)簽
那么現(xiàn)在的attrs.xml為:
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="testAttr" format="integer" /> </resources>
* MyTextView實(shí)現(xiàn)
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(); } }
貌似多了些代碼,可以看到我們聲明了一個(gè)int數(shù)組,數(shù)組中的元素就是我們想要獲取的attr的id。并且我們根據(jù)元素的在數(shù)組中的位置,定義了一些整形的常量代表其下標(biāo),然后通過TypedArray進(jìn)行獲取。
可以看到,我們?cè)镜模?/p>
R.styleable.test => mAttr R.styleable.test_text => ATTR_ANDROID_TEXT(0) R.styleable.test_testAttr => ATTR_TESTATTR(1)
那么其實(shí)呢?android在其內(nèi)部也會(huì)這么做,按照傳統(tǒng)的寫法,它會(huì)在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,根據(jù)上述你應(yīng)該發(fā)現(xiàn)了什么。styleale的出現(xiàn)系統(tǒng)可以為我們完成很多常量(int[]數(shù)組,下標(biāo)常量)等的編寫,簡化我們的開發(fā)工作(想想如果一堆屬性,自己編寫常量,你得寫成什么樣的代碼)。那么大家肯定還知道declare-styleable的name屬性,一般情況下寫的都是我們自定義View的類名。主要為了直觀的表達(dá),該declare-styleable的屬性,都是改View所用的。
其實(shí)了解該原理是有用的,詳見:Android 自定義控件 優(yōu)雅實(shí)現(xiàn)元素間的分割線
ok,現(xiàn)在5個(gè)問題,回答了4個(gè),第一個(gè)問題:
自定義屬性的幾個(gè)步驟是如何奏效的?
恩,上述以及基本涵蓋了這個(gè)問題的答案,大家自己總結(jié),所以:略。
總結(jié):
- attrs.xml里面的declare-styleable以及item,android會(huì)根據(jù)其在R.java中生成一些常量方便我們使用(aapt干的),本質(zhì)上,我們可以不聲明declare-styleable僅僅聲明所需的屬性即可。
- 我們?cè)赩iew的構(gòu)造方法中,可以通過AttributeSet去獲得自定義屬性的值,但是比較麻煩,而TypedArray可以很方便的便于我們?nèi)カ@取。
- 我們?cè)谧远xView的時(shí)候,可以使用系統(tǒng)已經(jīng)定義的屬性。
以上就是關(guān)于Android中的自定義屬性的相關(guān)內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Android應(yīng)用開發(fā)中自定義ViewGroup視圖容器的教程
- Android App中自定義View視圖的實(shí)例教程
- android 自定義控件 自定義屬性詳細(xì)介紹
- android開發(fā)教程之自定義屬性用法詳解
- Android中自定義控件的declare-styleable屬性重用方案
- Android自定義屬性 format的深入解析
- 詳解Android自定義控件屬性TypedArray以及attrs
- Android UI設(shè)計(jì)系列之自定義TextView屬性實(shí)現(xiàn)帶下劃線的文本框(4)
- 詳解Android自定義控件屬性
- Android如何自定義視圖屬性
相關(guān)文章
Android線性布局與相對(duì)布局的實(shí)現(xiàn)
大家好,本篇文章主要講的是Android線性布局與相對(duì)布局的實(shí)現(xiàn),感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02Android 自定義 View 中使用 Spannable的實(shí)例詳解
這篇文章主要介紹了Android 自定義 View 中使用 Spannable的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05淺談Android客戶端與服務(wù)器的數(shù)據(jù)交互總結(jié)
這篇文章主要介紹了淺談Android客戶端與服務(wù)器的數(shù)據(jù)交互總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Android?使用flow實(shí)現(xiàn)倒計(jì)時(shí)的方式
這篇文章主要介紹了Android?使用flow實(shí)現(xiàn)倒計(jì)時(shí)的方式,借助Flow這個(gè)工具,更加優(yōu)雅地實(shí)現(xiàn)這個(gè)需求功能,文末給大家整理了Android?實(shí)現(xiàn)倒計(jì)時(shí)的幾種方式,需要的朋友可以參考下2022-04-04Android 三種動(dòng)畫詳解及簡單實(shí)例
這篇文章主要介紹了Android 三種動(dòng)畫詳解及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04android實(shí)現(xiàn)在圖標(biāo)上顯示數(shù)字
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)在圖標(biāo)上顯示數(shù)字,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android Loader的使用以及手機(jī)通訊錄的獲取方法
下面小編就為大家分享一篇Android Loader的使用以及手機(jī)通訊錄的獲取方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01