標籤:
一、目標。
在布局檔案中使用自訂控制項時,直接在屬性中設定值,類似於在TextView控制項中設定text屬性來顯示文本。
效果
屬性設定:
二、代碼實現。
1、自訂命名空間,類似於TextView控制項裡面android:text屬性前的android。在需要放置自訂控制項的布局檔案的布局方式(LinearLayout、RelativeLayout等均可)屬性裡,參照android的命名空間樣式增加自訂的命名空間,其名稱可以隨便取(本例中取名custom),代碼是:xmlns:custom="http://schemas.android.com/apk/res/<包名>",其中包名是全稱(即配置AndroidManifest.xml裡面的package="<包名>")。
自訂命名空間代碼:
1 xmlns:custom="http://schemas.android.com/apk/res/com.example.mobilesafe"
View Code
因此,LinearLayout屬性裡的代碼為:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"2 xmlns:custom="http://schemas.android.com/apk/res/com.example.mobilesafe"3 android:layout_width="match_parent"4 android:layout_height="match_parent"5 android:orientation="vertical" >
View Code
2、定義屬性(本例中的title、checkbox_on、checkbox_off)。(可結合android原始碼中的TextView組件屬性的設定進行編寫,地址為sdk裡面的platforms\android-18\data\res\values.xml檔案中,尋找<declare-styleable name="TextView">的標籤,可照著裡面的 <attr name="password" format="boolean" />標籤進行編寫)。
①.在values檔案夾下建立一個xml檔案(注意是Android XML File類型檔案),取名為arrts。
②.在arrts.xml檔案中的<resources>標籤下建立 <declare-styleable>標籤,在 <declare-styleable>標籤下再建立<attr/>標籤。<attr/>標籤用於自訂屬性名和類型,所以它的數量可以根據需要增加,本例中未三個(title、checkbox_on、checkbox_off)。
③.在<attr/>標籤屬性中,增加name和format等標籤屬性,name的值就是自訂控制項屬性名稱(title、checkbox_on、checkbox_off),format的值就是自訂控制項的格式(均為string),其他的標籤屬性可參照TextView控制項的編寫。
attrs.xml檔案的代碼:
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <declare-styleable name="TextView"> 5 <attr name="title" format="string" /> 6 <attr name="check_off" format="string" /> 7 <attr name="check_on" format="string" /> 8 </declare-styleable> 9 10 </resources>
View Code
3、將定義的屬性與自訂控制項類關聯。自訂控制項(包括系統控制項TextView等)在放入布局檔案中時均需要執行個體化自訂控制項類(SettingItemView),此時會調用自訂控制項類中帶有兩個參數的構造方法SettingItemView(Context context, AttributeSet attrs),而帶有三個參數的構造方法在需要傳入樣式的時候調用,帶有一個參數的構造方法在new的時候調用。布局檔案在調用自訂控制項時會將裡面的屬性封裝成AttributeSet類型(attrs)的屬性集合(是個String類型的數組),因此可以通過AttributeSet類型(attrs)的相關方法取得其中的屬性。
①.在SettingItemView(Context context, AttributeSet attrs)方法中通過AttributeSet類型(attrs)的getAttributeValue(String namespace, String name)方法獲得屬性的值,該方法中參數namespace是命名空間,也就是想要獲得值的屬性所在的命名空間(本例中就是自訂(custom)的命名空間"http://schemas.android.com/apk/res/com.example.mobilesafe"),參數name是想要取值的屬性名稱。該方法的String類型傳回值就是所需屬性的值。
②.在SettingItemView類中定義String類型的常員變數(title、check_off、check_on),然後再將getAttributeValue方法返回的值賦給這些常員變數。
③.最後通過TextView對象(setting_update_title)的setText方法將②中的傳回值(title)填入,而另兩個傳回值(check_off、check_on)是根據CheckBox的狀態情況進行即時改變的,所以放在setChecked(boolean checked)方法中。
SettingItemView(Context context, AttributeSet attrs)方法的代碼如下:
1 public SettingItemView(Context context, AttributeSet attrs) {2 super(context, attrs);3 iniView(context);4 title = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.example.mobilesafe", "title");5 check_off = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.example.mobilesafe", "check_off");6 check_on = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.example.mobilesafe", "check_on");7 setting_update_title.setText(title);8 }View Code
④.根據CheckBox的狀態即時改變常值內容。在setChecked(boolean checked)方法中通過if..語句判斷CheckBox的狀態,再通過setContent(String text)方法並傳入傳回值從而設定顯示資訊。
setChecked(boolean checked)方法的代碼如下:
1 public void setChecked(boolean checked){2 if(checked){3 setContent(check_on);4 }else{5 setContent(check_off);6 }7 setting_update_checkbox.setChecked(checked);8 }View Code
三、擴充。
再定義兩個自訂控制項的屬性(content、checked)用於指定content的預設顯示內容和CheckBox的預設選中狀態(true或false)。提示:在關聯控制項和類時採用AttributeSet(arrts)的getAttributeBooleanValue方法用於返回boolean類型的值。
Android執行個體-手機安全衛士(九)-自訂群組合控制項的屬性