4. 自定義控制元件(4) --- 自定義屬性

weixin_34146805發表於2019-01-23

自定義屬性

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout          
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:wm="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <com.skyrayinstrument.wumeng.autoattribute.MyAttribute
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        wm:my_age="25"
        wm:my_name="WuMeng"
        wm:my_bg="@drawable/girl"/>

</android.support.constraint.ConstraintLayout>

上面是佈局檔案

  • 定義了三個自定義屬性
  • 注意名稱空間的問題
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!--定義名字叫MyAttribute屬性集合-->
    <declare-styleable name="MyAttribute">
        <!--定義一個名字叫my_age並且型別是integer的屬性-->
        <attr name="my_age" format="integer"/>
        <!--定義一個名字叫my_name並且型別是string的屬性-->
        <attr name="my_name" format="string"/>
        <!--定義一個名字叫my_bg並且型別是reference|color的屬性-->
        <attr name="my_bg" format="reference|color"/>
    </declare-styleable>

</resources>

上面是values/attrs.xml檔案的內容

  • 檔案的名字是attrs
package com.skyrayinstrument.wumeng.autoattribute;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * @author WuMeng
 * @date 2019/1/21
 * Describe: 自定義屬性
 */
public class MyAttribute extends View {

    private int myAge;
    private String myName;
    private Bitmap myBg;

    public MyAttribute(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        // 獲取XML檔案中的屬性,有三種方式
        // 1. 用名稱空間獲取
        String my_age = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","my_age");
        String my_name = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","my_name");
        String my_bg = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","my_bg");
        Log.d("WM","my_age ==" + my_age + "my_name ==" + my_name + "my_bg == " + my_bg);

        //2. 遍歷屬性集合
        for (int i = 0; i < attrs.getAttributeCount();i++) {
            Log.d("WM",""+attrs.getAttributeName(i)+"---"+attrs.getAttributeValue(i));
        }

        //3. 使用系統工具,獲取屬性
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyAttribute);
        for (int i = 0; i < typedArray.getIndexCount(); i++) {
            int index = typedArray.getIndex(i);
            switch (index) {
                case R.styleable.MyAttribute_my_age:
                    myAge = typedArray.getInt(index,0);
                    break;
                case R.styleable.MyAttribute_my_name:
                    myName = typedArray.getString(index);
                    break;
                case R.styleable.MyAttribute_my_bg:
                    Drawable drawable = typedArray.getDrawable(index);
                    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
                    myBg = bitmapDrawable.getBitmap();
                    break;
                default:
                    break;
            }
        }
        // 記得回收
        typedArray.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        canvas.drawText("" + myName+": " + myAge,50,50,paint);
        canvas.drawBitmap(myBg,100,100,paint);
    }
}

上面是獲取XML佈局檔案中的自定義屬性值的三種方法

相關文章