Android應用資源系列之屬性(Attribute)資源

屬性(Attribute)資源:屬於整個Android應用資源的一部分.其實就是網上一堆介紹怎麼給自定義View添加自己的屬性文章裏的attrs文件,此文件位於../res/values/目錄下 


當別人通過XML文件配置的方式來創建你開發的自定義組件,並且還能動態設置你自定義組件的屬性時,這時候你就需要給你自己自定義的組件配上一個XML屬性資源文件來完成這項工作了. 

其實也可以不配上一個XML屬性資源文件,也能完成如上的功能,這樣你的自定義組件顯得更乾脆一點,一個自定義組件就是一個類文件,不拖泥帶水的。但是這兩種方式是有區別的,具體看自己的需求來選擇了,以下爲兩種實現方式: 


1:配合XML屬性資源文件的方式 
  第一步:attrs.xml文件 
  首先當然是要寫出自定義組件的類文件了,然後將這個類裏需要外界傳入值的屬性定義成一個屬性資源文件. 
  在工程裏的../res/values/目錄下創建一個attrs.xml文件,文件名並不是只能寫成這樣,這樣寫只有一個目的,別人一看就知道這個文件是屬性資源文件了,具體寫法如下: 

Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.         <attr name="test1" format="string" />    
  5.   
  6.     <declare-styleable name="MyView">  
  7.               <attr name="textColor" format="color" />    
  8.               <attr name="textSize" format="dimension" />    
  9.               <attr name="text" format="string" />  
  10.         </declare-styleable>    
  11. </resources>  

attrs.xml解釋如下 
Java代碼  收藏代碼
  1. attr子元素:  
  2.   
  3. 定義具體的屬性,format表示這個屬性的值的類型,類型有以下幾種:  
  4.      1.reference:參考指定Theme中資源ID,這個類型意思就是你傳的值可以是引用資源  
  5.      2.string:字符串,如果你想別人既能直接寫值也可以用類似"@string/test"引用資源的方式,可以寫成format="string|reference"  
  6.      3.Color:顏色  
  7.      4.boolean:布爾值  
  8.      5.dimension:尺寸值  
  9.      6.float:浮點型  
  10.      7.integer:整型  
  11.      8.fraction:百分數  
  12.      9.enum:枚舉 ,如果你提供的屬性只能讓別人選擇,不能隨便傳入,就可以寫成這樣  
  13.         <attr name="language">  
  14.                 <enum name="china" value="1"/>  
  15.                 <enum name="English" value="2"/>  
  16.             </attr>  
  17.      10.flag:位或運算  
  18.   
  19.   
  20. declare-styleable子元素:  
  21.   
  22. 定義一個styleable對象,每個styleable對象就是一組attr屬性的集合 注意:這裏的name屬性並不是一定要和自定義類名相同,只是爲了好區分對應類的屬性而已  
  23.   
  24.   
  25. 注意:上面的屬性資源文件定義了該屬性之後,至於到底是哪個自定義View組件中來使用該屬性,該屬性到底能發揮什麼作用, 就不歸該屬性資源文件管了,也就是說這個屬性資源文件是個公共的,大家都可以用,但是爲了方便管理,一般都是一個自定義View裏的屬性寫成一個declare-styleable集合.屬性資源所定義的屬性到底可以返回什麼作用,取決於自定義組件的代碼實現  


    第二步:在自定義類裏引用attrs文件裏定義的屬性爲自己的屬性設置值 

Java代碼  收藏代碼
  1. package cn.com.androidtest.ui;  
  2.   
  3. import cn.com.androidtest.R;  
  4. import android.content.Context;  
  5. import android.content.res.TypedArray;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Color;  
  8. import android.graphics.Paint;  
  9. import android.graphics.Paint.Style;  
  10. import android.graphics.Rect;  
  11. import android.util.AttributeSet;  
  12. import android.view.View;  
  13.   
  14. public class MyView extends View  
  15. {  
  16.   
  17.      private Paint mPaint;    
  18.      private Context mContext;    
  19.      private static String mString;  
  20.      private String test;  
  21.          
  22.     public MyView(Context context)   
  23.     {    
  24.           super(context);    
  25.             mPaint = new Paint();    
  26.     }    
  27.       
  28.     public MyView(Context context,AttributeSet attrs)    
  29.     {    
  30.             super(context,attrs);    
  31.             mPaint = new Paint();    
  32.               
  33.                 /*這裏取得declare-styleable集合*/  
  34.              TypedArray typeArray = context.obtainStyledAttributes(attrs,R.styleable.MyView);     
  35.                  /*這裏從集合裏取出相對應的屬性值,第二參數是如果使用者沒用配置該屬性時所用的默認值*/  
  36.             int textColor = typeArray.getColor(R.styleable.MyView_textColor,0XFFFFFFFF);    
  37.             float textSize = typeArray.getDimension(R.styleable.MyView_textSize, 36);    
  38.             mString = typeArray.getString(R.styleable.MyView_text);  
  39.              /*設置自己的類成員變量*/  
  40.             mPaint.setTextSize(textSize);    
  41.             mPaint.setColor(textColor);    
  42.             /*關閉資源*/  
  43.             typeArray.recycle();    
  44.     }    
  45.     @Override    
  46.     protected void onDraw(Canvas canvas)   
  47.     {    
  48.          super.onDraw(canvas);    
  49.   
  50.          mPaint.setStyle(Style.FILL);          
  51.          canvas.drawRect(new Rect(10109090), mPaint);          
  52.          mPaint.setColor(Color.BLUE);     
  53.          canvas.drawText(mString, 10110, mPaint);    
  54.     }    
  55. }  


第三步:使用自定義組件,並設置屬性 

Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:myandroid="http://schemas.android.com/apk/res/cn.com.androidtest"  
  5.     android:orientation="vertical"  
  6.     android:layout_width="fill_parent"  
  7.     android:layout_height="fill_parent"  
  8.     >  
  9. <TextView    
  10.     android:layout_width="fill_parent"   
  11.     android:layout_height="wrap_content"   
  12.     android:text="@string/hello"/>  
  13.       
  14.     <cn.com.androidtest.ui.MyView  
  15.          android:layout_width="fill_parent"   
  16.          android:layout_height="wrap_content"   
  17.          myandroid:textColor="#ff0000"  
  18.          myandroid:textSize="20px"  
  19.          myandroid:text="http://wujiandong.iteye.com"/>  
  20. </LinearLayout>  


注意:java代碼裏那種取屬性值的方式,那麼在XML使用該組件的時候一定要爲該自定義組件設置一個命名空間[xmlns:myandroid="http://schemas.android.com/apk/res/cn.com.androidtest"],不然組件屬性設置不了 
命名空間寫法:xmlns:空間名="http://schemas.android.com/apk/res/自定義組件所在包名" 
寫包名時候也有個要注意的地方: 
如果你的自定義View所在包類似如下兩圖,那麼包名只能寫成最頂層包[cn.com.androidtest],而不能是[cn.com.androidtest.ui] 


 

 

第四步:終於完成了,看下效果圖 

 



2:不需要配合XML資源文件的方式 
基本與第一種方式相同,只是java代碼取屬性值的部分,和別人用你自義View時有點區別 

java代碼寫法: 

Java代碼  收藏代碼
  1. /*資源ID號引用*/  
  2.         int resouceId = -1;  
  3.         /*取得資源ID號,第一個參數:命名空間名.第二個參數:xml文件裏設置的屬性名.第三個參數:默認值*/  
  4.         resouceId = attrs.getAttributeResourceValue(null"textColor"0);  
  5.         if (resouceId > 0)  
  6.             textColor = context.getResources().getColor(resouceId);  
  7.         resouceId = attrs.getAttributeResourceValue(null"textColor"0);  
  8.         if(resouceId > 0)  
  9.             mString = context.getResources().getText(resouceId, "http://wujiandong.iteye.com").toString();  


XML中使用的時候,命名空間可以不要,屬性名就要自己對應好了,不然程序取不到,不像有XML資源文件配合的方式有個約束.絕對自由等於沒有自由~~ 
Xml代碼  收藏代碼
  1. <cn.com.androidtest.ui.MyView  
  2.          android:layout_width="fill_parent"   
  3.          android:layout_height="wrap_content"   
  4.          textColor="#ff0000"  
  5.          textSize="20px"  
  6.          text="http://wujiandong.iteye.com"/>  
發佈了63 篇原創文章 · 獲贊 69 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章