安卓自定義屬性attrs

1.在res/values目錄下定義attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="CustomView">
        <attr name="select_text_size" format="dimension" />
        <attr name="isSelect" format="boolean" />
        <attr name="defaultColor" format="color" />
        <attr name="duration" format="integer" />
        <attr name="text" format="string|reference" />
    </declare-styleable>

</resources>
attr子元素:  

定義具體的屬性,format表示這個屬性的值的類型,類型有以下幾種:  
     1.reference:參考指定Theme中資源ID,這個類型意思就是你傳的值可以是引用資源  
     2.string:字符串,如果你想別人既能直接寫值也可以用類似"@string/test"引用資源的方式,可以寫成format="string|reference"  
     3.Color:顏色  
     4.boolean:布爾值  
     5.dimension:尺寸值  
     6.float:浮點型  
     7.integer:整型  
     8.fraction:百分數  
     9.enum:枚舉 ,如果你提供的屬性只能讓別人選擇,不能隨便傳入,就可以寫成這樣  
        <attr name="language">  
                <enum name="china" value="1"/>  
                <enum name="English" value="2"/>  
            </attr>  
     10.flag:位或運算  


declare-styleable子元素:  

定義一個styleable對象,每個styleable對象就是一組attr屬性的集合 注意:這裏的name屬性並不是一定要和自定義類名相同,只是爲了好區分對應類的屬性而已  


注意:上面的屬性資源文件定義了該屬性之後,至於到底是哪個自定義View組件中來使用該屬性,該屬性到底能發揮什麼作用, 就不歸該屬性資源文件管了,也就是說這個屬性資源文件是個公共的,大家都可以用,但是爲了方便管理,一般都是一個自定義View裏的屬性寫成一個declare-styleable集合.屬性資源所定義的屬性到底可以返回什麼作用,取決於自定義組件的代碼實現 

2.自定義類中使用


public class MyView extends View {

    private Paint mPaint;
    private static String mString;

    public MyView(Context context) {
        super(context);
        mPaint = new Paint();
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();

        /* 這裏取得declare-styleable集合 */
        TypedArray typeArray = context.obtainStyledAttributes(attrs,
                R.styleable.CustomView);
        /* 這裏從集合裏取出相對應的屬性值,第二參數是如果使用者沒用配置該屬性時所用的默認值 */
        int textColor = typeArray.getColor(
                R.styleable.CustomView_defaultColor, 0xff000000);
        float textSize = typeArray.getDimension(
                R.styleable.CustomView_select_text_size, 36);
        mString = typeArray.getString(R.styleable.CustomView_text);
        /* 設置自己的類成員變量 */
        mPaint.setTextSize(textSize);
        mPaint.setColor(textColor);
        /* 關閉資源 */
        typeArray.recycle();
    }

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setStyle(Style.FILL);
        canvas.drawRect(new Rect(10, 10, 190, 90), mPaint);
        mPaint.setColor(Color.BLUE);
        canvas.drawText("aaaa" + mString, 100, 110, mPaint);
    }

}

3.使用自定義屬性

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:myandroid="http://schemas.android.com/apk/res/com.example.myview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <com.example.myview.MyView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        myandroid:select_text_size="12sp"
        myandroid:isSelect="true"
        myandroid:text="@string/hello_world" >
    </com.example.myview.MyView>

</RelativeLayout>

注意:java代碼裏那種取屬性值的方式,那麼在XML使用該組件的時候一定要爲該自定義組件設置一個命名空間[xmlns:myandroid=”http://schemas.android.com/apk/res/cn.com.androidtest”],不然組件屬性設置不了
命名空間寫法:xmlns:空間名=”http://schemas.android.com/apk/res/自定義組件所在包名” ;包名是最頂層的包名

myandroid是自己定義的,和下面myandroid:text中的myandroid保持一致即可。

參考文章:
http://wujiandong.iteye.com/blog/1184921

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