Android-自定义属性

在Android开发中,大多数都是用Android提供的属性,例如:

android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40dp"

这些都是Android定义的,只是在使用Android定义的属性,现在我们自己来自定义属性

在自定义属性之前,先去了解Android是如何自定义属性的:需要找到SDK目录中(D: oolssdkplatformsandroid-28data esvalues)

attrs.xml里面就是定义了Android的属性规则:

name为属性名称,format为类型

自己自定义属性:

myattribute:my_age="26"

myattribute:my_name="刀郎"

myattribute:my_bg="@mipmap/jtx"

注意:需要申请:xmlns:myattribute="http://schemas.android.com/apk/res-auto"

<!-- 自定义属性 -->
<LinearLayout 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:myattribute="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context=".ShangGuiguTestActivity">

    <view.custom.shangguigucustomview.MyCustomAttribute
        myattribute:my_age="26"
        myattribute:my_name="刀郎"
        myattribute:my_bg="@mipmap/jtx"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

 编写 attrs.xml文件,来规则属性类型:

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

    <declare-styleable name="MyCustomAttribute">

        <attr name="my_age" format="integer" />
        <attr name="my_name" format="string" />
        <attr name="my_bg" format="reference" />

    </declare-styleable>

</resources>

使用自定义属性:

public class MyCustomAttribute extends View {

    private static final String TAG  = MyCustomAttribute.class.getSimpleName();

    private Paint paint;

    public MyCustomAttribute(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true); // 去锯齿
        paint.setTextSize(60);

        initView(context, attrs);
    }

    private int myage;
    private String myname;
    private Drawable mybg;

    private void initView(Context context, AttributeSet attributeSet) {

        // 1.通过命名控件来获取
        /*String age = attributeSet.getAttributeValue("http://schemas.android.com/apk/res-auto", "my_age");
        String name = attributeSet.getAttributeValue("http://schemas.android.com/apk/res-auto", "my_name");
        String bg = attributeSet.getAttributeValue("http://schemas.android.com/apk/res-auto", "my_bg");

        Log.i(TAG, "age:" + age + " name:" + name + " bg:" + bg);*/

        // 2.通过变量属性方式打印获取
        /*for (int i=0; i<attributeSet.getAttributeCount(); i++) {
            Log.i(TAG, "name:" + attributeSet.getAttributeName(i) + " value:" + attributeSet.getAttributeValue(i));
        }*/

        // 3.通过控件方式来获取,比较靠谱,这种方式才可以把图片显示
        TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.MyCustomAttribute);
        myage = typedArray.getInt(R.styleable.MyCustomAttribute_my_age, 0);
        myname = typedArray.getString(R.styleable.MyCustomAttribute_my_name);
        mybg = typedArray.getDrawable(R.styleable.MyCustomAttribute_my_bg);

        typedArray.recycle(); // 因为源码中是进行回收的

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // 测量 宽度  高度
        setMeasuredDimension(1200, 3000);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 绘制我的年龄相关信息
        canvas.drawText(String.valueOf(myage), 60, 100, paint);

        // 绘制我的名称相关信息
        canvas.drawText(myname, 60, 180, paint);

        // 绘制图片
        canvas.drawBitmap(((BitmapDrawable)mybg).getBitmap(), 60, 250, paint);
    }
}

 效果图:

原文地址:https://www.cnblogs.com/android-deli/p/9648756.html