android 之 Attr的使用

TypedArray的作用:

Container for an array of values that were retrieved with obtainStyledAttributes(AttributeSet, int[], int, int) or obtainAttributes(AttributeSet, int[]). Be sure to call recycle() when done with them. The indices used to retrieve values from this structure correspond to the positions of the attributes given to obtainStyledAttributes.

是一個用於存放恢復obtainStyledAttributes(AttributeSet, int[], int, int)或 obtainAttributes(AttributeSet, int[])  值的一個數組容器,當操作完成以後,一定要調用recycle()方法。用於檢索的索引值在這個結構對應的位置給obtainStyledAttributes屬性。

使用這個類的時候,先要在valuse文件夾下創建:atts.xml文件:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="FlowIndicator">  
  4.         <attr name="count" format="integer" />  
  5.         <attr name="space" format="dimension" />  
  6.         <attr name="point_size" format="dimension" />  
  7.         <attr name="point_seleted_color" format="color|reference" />  
  8.         <attr name="point_normal_color" format="color|reference" />  
  9.         <attr name="point_radius" format="dimension" />  
  10.     </declare-styleable>  
  11. </resources>  
         首先,聲明自定義<declare-styleable name="FlowIndicator">,nameFlowIndicator,屬性設置爲比較簡單的格式,前面參數name,後面是參數格式。

自定義屬性的format,可以有以下多種:

  • reference
  • string
  • color
  • dimension
  • boolean
  • integer
  • float
  • fraction
  • enum
  • flag

然後這樣使用:

[java] view plaincopy
  1. public FlowIndicator(Context context, AttributeSet attrs) {  
  2.         super(context, attrs);  
  3.         //獲得實例  
  4.         TypedArray typeArray = context.obtainStyledAttributes(attrs,  
  5.                 R.styleable.FlowIndicator);  
  6.         //從typeArray獲取相應值,第二個參數爲默認值,如第一個參數在atts.xml中沒有定義,返回第二個參數值  
  7.         count = typeArray.getInteger(R.styleable.FlowIndicator_count, 4);  
  8.         space = typeArray.getDimension(R.styleable.FlowIndicator_space, 9);  
  9.         radius = typeArray.getDimension(R.styleable.FlowIndicator_point_radius, 9);  
  10.   
  11.         point_normal_color = typeArray.getColor(  
  12.                 R.styleable.FlowIndicator_point_normal_color, 0x000000);  
  13.         point_seleted_color = typeArray.getColor(  
  14.                 R.styleable.FlowIndicator_point_seleted_color, 0xffff07);  
  15.   
  16.         int sum = attrs.getAttributeCount();  
  17.         if (Constans.DEBUG) {  
  18.             String str = "";  
  19.             for (int i = 0; i < sum; i++) {  
  20.                 String name = attrs.getAttributeName(i);  
  21.                 String value = attrs.getAttributeValue(i);  
  22.                 str += "attr_name :" + name + ": " + value + "\n";  
  23.             }  
  24.             Log.i("attribute", str);  
  25.         }  
  26.         typeArray.recycle();  
  27.     }  

最後一定不要忘記typeArray.recycle():

[java] view plaincopy
  1. Give back a previously retrieved StyledAttributes, for later re-use.  

反回以前提取的styledattributes,以後再使用。

在獲取屬性值的時候所用類似R.styleable.FlowIndicator_count的方式,其中FlowIndicator_count是採取的名字_屬性這種格式。定義好了自定義屬性,就可以在自定控件中的屬性設置了:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res/com.dream.myqiyi"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.         <com.dream.myqiyi.widget.FlowIndicator  
  8.             android:id="@+id/myView"  
  9.             android:layout_width="fill_parent"  
  10.             android:layout_height="wrap_content"  
  11.             android:layout_marginBottom="5dip"  
  12.             app:count="4"  
  13.             android:gravity="center"  
  14.             app:point_normal_color="#45000000"  
  15.             app:point_radius="3dip"  
  16.             app:point_seleted_color="#ffffff"  
  17.             app:point_size="5dip"  
  18.             app:space="10dip" />  
  19. </FrameLayout>  
首先,要有聲明:xmlns:app="http://schemas.android.com/apk/res/com.dream.myqiyi",“com.dream.myqiyi”這個是你項目的包名。

然後我們就可以使用app:這樣設置自定義的屬性了。

如果控件繼承自某個View類,那麼在定義屬性的時候就不用考慮佈局的屬性的了;

發佈了21 篇原創文章 · 獲贊 6 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章