Android自訂控制項——自訂屬性
我們在自訂android組件的時候,除了用Java構建出組件的樣子外,有時候還需要去申明一些“屬性”提供給項目使用,那麼什麼是組件的屬性呢?
例如在資訊清單檔中,建立一個TextView的時候,這是需要制定TextView的android:layout_width=wrap_content android:layout_height=wrap_content等等這些都是組件的屬性,TextView是android系統為我們提供好的組件,它的屬性亦是android系統為我們提供了。詳情查看android的源碼,我這裡舉例android2.3的源碼,路徑是
/frameworks/base/core/res/res/values/attrs.xml,這個attrs.xml定義了所有android系統組件的屬性。
當我們自訂群組件時,除了可以使用android系統為我們提供好的屬性之外,還可以自訂屬性。自訂屬性主要步驟如下:
一、在attrs.xml檔案中聲明屬性,如:
// 聲名屬性集的名稱,即這些屬性是屬於哪個控制項的。 // 聲名屬性 current_state 格式為 boolean 類型 // 聲名屬性 slide_button格式為 reference 類型
所有的format類型
reference 引用
color 顏色
boolean 布爾值
dimension 尺寸值
float 浮點值
integer 整型值
string 字串
enum 枚舉值
二、在布局檔案中使用:在使用之前必須聲名命名空間,xmlns:example=http://schemas.android.com/apk/res/com.example.mytogglebtn
說明:xmlns 是XML name space 的縮寫;
example 可為任意寫符
http://schemas.android.com/apk/res/ 此為android固定格式;
com.example.mytogglebtn 此應用的包名,如manifest設定檔中一致。
布局檔案:
三、在代碼中對屬性進行解析,代碼如下:
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyToggleBtn);// 由attrs 獲得 TypeArray
以上是建立自訂屬性的大致步驟。下面,我將要建立一個自訂控制項的Demo,來學習學習自訂屬性的相關知識點。
首先,需要建立一個自訂控制項出來,並且繼承View。在工程的res/values檔案夾下建立attrs.xml檔案:
然後在布局檔案中,引用這個自訂控制項MyView
由於建立出來的自訂群組件MyView是繼承於View的,所以必須得複寫View的構造方法,View中有三個構造方法,先來看看複寫帶一個參數的構造方法:
package com.example.myattrs;import android.content.Context;import android.view.View;public class MyView extends View {public MyView(Context context) {super(context);// TODO Auto-generated constructor stub}}運行一下工程,那麼工程立即崩潰了,報錯也很清晰明了:
09-17 06:52:24.389: E/AndroidRuntime(1563): Caused by: java.lang.NoSuchMethodException: [class android.content.Context, interface android.util.AttributeSet]
表示沒有找到某個帶兩個參數的構造方法,於是,知道自訂屬性必須得複寫父類的另外一個構造方法,修改如下:
package com.example.myattrs;import android.content.Context;import android.util.AttributeSet;import android.view.View;public class MyView extends View {public MyView(Context context, AttributeSet attrs) {super(context, attrs);int count = attrs.getAttributeCount();for (int index = 0; index < count; index++) {String attributeName = attrs.getAttributeName(index);String attributeValue = attrs.getAttributeValue(index);System.out.println(name: + attributeName + value: + attributeValue);}}}列印結果如下:
AttributeSet:對布局檔案XML解析後的結果,封裝為AttributeSet對象。儲存的都是未經處理資料,但是對資料進行了簡單的加工。
由此構造器幫我們返回了布局檔案XML的解析結果,拿到這個結果,我們該怎麼做呢?接下來,我們來看看View類對於這個是怎麼處理的:
public View(Context context, AttributeSet attrs) { this(context, attrs, 0); }
public View(Context context, AttributeSet attrs, int defStyle) { this(context); TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View, defStyle, 0);於是,找到一個跟屬性很相關的類TypeArray,那麼接下來,我在自訂控制項的構造方法上也擷取一下TypeArray這個類:
翻看一下TypeArray的源碼會發現,TypeArray是不繼承任何類(除了Object)的,也就是說,TypeArray相當於一個工具類,通過context.obtainStyledAttributes方法,將AttributeSet和屬性的類型傳遞進去,比如AttributeSet相當於原材料,屬性類型相當於圖紙,context.obtainStyledAttributes相當於加工廠加工成所對象的屬性,封裝到TypeArray這個類裡。
package com.example.myattrs;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.view.View;public class MyView extends View {public MyView(Context context, AttributeSet attrs) {super(context, attrs);TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyView);int count = ta.getIndexCount();for (int i = 0; i < count; i++) {int itemId = ta.getIndex(i);System.out.println(itemId:: + itemId); // 擷取屬性在R.java檔案中的idswitch (itemId) {case R.styleable.MyView_test_bitmap:int bitmapId = ta.getResourceId(itemId, 100);System.out.println(bitmapId:: + bitmapId);break;case R.styleable.MyView_test_id:int test_id = ta.getInteger(itemId, 10);System.out.println(test_id + test_id);break;case R.styleable.MyView_test_msg:String test_msg = ta.getString(itemId);System.out.println(test_msg:: + test_msg);break;default:break;}}}}
以下是TypeArray類裡的方法,這裡不寫注釋了,見名知意:
當在構造方法中擷取到這些設定好的屬性值時,取出其值,就可以在代碼中進行處理了。
上篇部落格提到了Android自訂控制項——仿ios的滑動開關按鈕,接下來,就要為這個滑動開關按鈕條件自訂的屬性,不熟悉上篇部落格Demo的,可以先去瀏覽器一下我的上篇部落格,點這裡Android自訂控制項——仿ios滑動開關按鈕
首先,按照上面介紹的步驟,先在res/values目錄下建立一個屬性檔案attrs.xml:
然後,在引用自訂控制項的布局檔案acticity_main.xml上設定自訂屬性,記住,引用這些屬性之前,必須先引用命名空間:
xmlns:mytogglebtn=http://schemas.android.com/apk/res/com.example.slidebutton
其中:mytogglebtn 是任意取名,沒有強制要求,但是在控制項中引用屬性的時候,要保持一致,不要寫錯了
com.example.slidebutton 是工程的包名,千萬不要弄錯了,不然找不到屬性檔案
有了上面的步驟,我們就可以自訂群組件類的構造方法中,將屬性集解析成TypeArray了,從TypeArray中擷取相關的屬性值,並用於初始化自訂控,以下是主要代碼:
public SlideButton(Context context, AttributeSet attrs) {super(context, attrs);// 獲得自訂屬性TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyToggleBtn);int count = ta.getIndexCount();for (int i = 0; i < count; i++) {int itemId = ta.getIndex(i); // 擷取某個屬性的Id值switch (itemId) {case R.styleable.MyToggleBtn_currState: // 設定當前按鈕的狀態currentState = ta.getBoolean(itemId, false);break;case R.styleable.MyToggleBtn_switchBG: // 設定按鈕的背景圖int backgroundId = ta.getResourceId(itemId, -1);if (backgroundId == -1)throw new RuntimeException(資源沒有被找到,請設定背景圖);switchBG = BitmapFactory.decodeResource(getResources(), backgroundId);break;case R.styleable.MyToggleBtn_slideBg: // 設定按鈕圖片int slideId = ta.getResourceId(itemId, -1);if (slideId == -1)throw new RuntimeException(資源沒有找到,請設定按鈕圖片);slideButtonBG = BitmapFactory.decodeResource(getResources(), slideId);break;default:break;}}}
從上可以看到,自訂屬性其實很簡單。就是在構造方法中,將擷取到的屬性集加工成TypeArray對象,通過這個對象取出屬性的id,通過id取出每個屬性對應的值(畢竟Android下的布局檔案XML也是key-value形式的),最後將擷取到的屬性值(控制項使用者自訂的資料)初始化到自訂控制項上,這樣,一個完整的自訂控制項就完成。這種完整的自訂控制項方式用的並不多見,因為在開發自訂控制項時候,需要什麼資料就直接在Java代碼裡設定就好了,方便多了。但是在特定的場合下,如果開發的控制項某些資料不確定,或者所開發控制項需要提供給其他人進行喜好設定什麼的,這種自訂屬性就顯得非用不可了。
源碼請在這裡下載