天天看點

android自定義視圖屬性(atts.xml,TypedArray)學習

       最近在學習過程中遇到這個問題,不知道TypedArray是幹嘛用的?去官方網站看一下吧:TypedArray繼承自Object類,然後再看下它的類概述:

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檔案:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="FlowIndicator">
        <attr name="count" format="integer" />
        <attr name="space" format="dimension" />
        <attr name="point_size" format="dimension" />
        <attr name="point_seleted_color" format="color|reference" />
        <attr name="point_normal_color" format="color|reference" />
        <attr name="point_radius" format="dimension" />
    </declare-styleable>
</resources>
           

         首先,聲明自定義<declare-styleable name="FlowIndicator">,nameFlowIndicator,屬性設定為比較簡單的格式,前面參數name,後面是參數格式。

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

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

然後這樣使用:

public FlowIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		//獲得執行個體
		TypedArray typeArray = context.obtainStyledAttributes(attrs,
				R.styleable.FlowIndicator);
		//從typeArray擷取相應值,第二個參數為預設值,如第一個參數在atts.xml中沒有定義,傳回第二個參數值
		count = typeArray.getInteger(R.styleable.FlowIndicator_count, 4);
		space = typeArray.getDimension(R.styleable.FlowIndicator_space, 9);
		radius = typeArray.getDimension(R.styleable.FlowIndicator_point_radius, 9);

		point_normal_color = typeArray.getColor(
				R.styleable.FlowIndicator_point_normal_color, 0x000000);
		point_seleted_color = typeArray.getColor(
				R.styleable.FlowIndicator_point_seleted_color, 0xffff07);

		int sum = attrs.getAttributeCount();
		if (Constans.DEBUG) {
			String str = "";
			for (int i = 0; i < sum; i++) {
				String name = attrs.getAttributeName(i);
				String value = attrs.getAttributeValue(i);
				str += "attr_name :" + name + ": " + value + "\n";
			}
			Log.i("attribute", str);
		}
		typeArray.recycle();
	}
           

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

Give back a previously retrieved StyledAttributes, for later re-use.
           

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

應該注意到,擷取屬性的時候所用的R.styleable.FlowIndicator_count中的FlowIndicator_count是采取的名字_屬性這種格式。

定義好了自定義屬性,就可以在自定控件中的屬性設定了:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.dream.myqiyi"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
        <com.dream.myqiyi.widget.FlowIndicator
            android:id="@+id/myView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dip"
            app:count="4"
            android:gravity="center"
            app:point_normal_color="#45000000"
            app:point_radius="3dip"
            app:point_seleted_color="#ffffff"
            app:point_size="5dip"
            app:space="10dip" />
</FrameLayout>
           

首先,要有聲明:

 xmlns:app="http://schemas.android.com/apk/res/com.dream.myqiyi",“com.dream.myqiyi”這個是你項目的包名。

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

app:point_normal_color="#45000000"
            app:point_radius="3dip"
            app:point_seleted_color="#ffffff"
            app:point_size="5dip"
            app:space="10dip"
           

先簡單的寫到這吧,如果問題,歡迎探讨。

繼續閱讀