Android自定義屬性的使用

 在android中我們習慣了在XML佈局文件中,進行控件屬性的設置,由於控件默認提供的屬性數量有限,爲了增加屬性我們可以給控件添加一些自定義的屬性,下面來講一下爲控件添加自定義屬性的幾個步驟。

1>在res/values文件下添加一個attrs.xml文件(沒有的話)如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <!--添加了一個ImageTextButton的屬性集  --> 
  4.     <declare-styleable name="ImageTextButton"> 
  5.         <attr format="reference" name="iconImage" /> 
  6.         <attr format="reference" name="bkImage" /> 
  7.         <attr format="integer" name="borderLeft" /> 
  8.         <attr format="integer" name="borderRight" /> 
  9.         <attr format="integer" name="borderTop" /> 
  10.         <attr format="integer" name="borderBottom" /> 
  11.         <attr format="integer" name="buttonstate" /> 
  12.         <attr name="iconLocation"> 
  13.             <enum name="center" value="0" /> 
  14.             <enum name="left" value="1" /> 
  15.             <enum name="right" value="2" /> 
  16.         </attr> 
  17.     </declare-styleable> 
  18. </resources> 

2>在相關的XML佈局文件中使用自定義的屬性:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:mux="http://schemas.android.com/apk/res/com.shareboard"  
  4.     android:layout_width="530dp"  
  5.     android:layout_height="320dp"  
  6.     android:background="@color/dlgBg"  
  7.     android:orientation="vertical" >  
  8. <!--com.shareboard 爲工程所在包  --> 
  9.             <com.shareboard.uicontrols.ImageTextButton  
  10.                 android:id="@+id/btnCancel"  
  11.                 android:layout_width="100sp"  
  12.                 android:layout_height="wrap_content"  
  13.                 android:layout_alignParentRight="true"  
  14.                 android:layout_marginRight="20dp"  
  15.                 mux:bkImage="@drawable/cell_bkgnd"  
  16.                 mux:buttonstate="2"  
  17.                 android:text="@string/btn_cancel"  
  18.                 android:textColor="@color/btnText"  
  19.                 android:textSize="@dimen/btnText" />  
  20.   
  21.           <com.shareboard.uicontrols.ImageTextButton  
  22.                 android:id="@+id/btnDone"  
  23.                 android:layout_width="100sp"  
  24.                 android:layout_height="wrap_content"  
  25.                 android:layout_alignParentLeft="true"  
  26.                 android:layout_marginLeft="@dimen/btnMargin"  
  27.                 mux:bkImage="@drawable/cell_bkgnd"  
  28.                 mux:buttonstate="2"  
  29.                 android:text="@string/btn_done"  
  30.                 android:textColor="@color/btnText"  
  31.                 android:textSize="@dimen/btnText" />  
  32. </LinearLayout>  

3>在代碼中獲取自定義的屬性值: 

  1. public final class ImageTextButton extends Button { 
  2.     private int mIconId; 
  3.     private int mBkimgId; 
  4.     private int mBorderLeft = 10
  5.     private int mBorderRight = 10
  6.     private int mBorderTop = 10
  7.     private int mBorderBottom = 10
  8.     private int mnButtonState = 4
  9.     private int mIconLocation = 0
  10.     private boolean mbChecked = false
  11.  
  12.     public ImageTextButton(Context context) { 
  13.         super(context); 
  14.         setClickable(true); 
  15.     } 
  16.  
  17.     public ImageTextButton(Context context, AttributeSet attrs) { 
  18.         super(context, attrs); 
  19.         readAttrs(context, attrs); 
  20.         setClickable(true); 
  21.     } 
  22.  
  23.     private void readAttrs(Context context, AttributeSet attrs) { 
  24.         TypedArray types = context.obtainStyledAttributes(attrs, 
  25.                 R.styleable.ImageTextButton); 
  26.         final int count = types.getIndexCount(); 
  27.         for (int i = 0; i < count; ++i) { 
  28.             int attr = types.getIndex(i); 
  29.             switch (attr) { 
  30.             case R.styleable.ImageTextButton_iconImage: 
  31.                 mIconId = types.getResourceId(attr, 0); 
  32.                 break
  33.             case R.styleable.ImageTextButton_bkImage: 
  34.                 mBkimgId = types.getResourceId(attr, 0); 
  35.                 break
  36.             case R.styleable.ImageTextButton_borderLeft: 
  37.                 mBorderLeft = types.getInteger(attr, 10); 
  38.                 break
  39.             case R.styleable.ImageTextButton_borderRight: 
  40.                 mBorderRight = types.getInteger(attr, 10); 
  41.                 break
  42.             case R.styleable.ImageTextButton_borderTop: 
  43.                 mBorderTop = types.getInteger(attr, 10); 
  44.                 break
  45.             case R.styleable.ImageTextButton_borderBottom: 
  46.                 mBorderBottom = types.getInteger(attr, 10); 
  47.                 break
  48.             case R.styleable.ImageTextButton_iconLocation: 
  49.                 mIconLocation = types.getInteger(attr, 0); 
  50.                 break
  51.             case R.styleable.ImageTextButton_buttonstate: 
  52.                 mnButtonState = types.getInteger(attr, 4); 
  53.                 break
  54.             } 
  55.         } 
  56.         types.recycle(); 
  57.     } 

好了這就完成了自定義屬性的定義和使用。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章