Android 自定义控件属性

前言

自定义控件经常需要一些特殊的配置,添加一些自定义属性。

1. 自定义属性

attrs.xml文件,所有自定义属性需要在文件中添加declare-styleable节点来声明,例如定义属性background_color设置背景色。

<declare-styleable name="AttrDeclareView">
    <attr name="background_color" format="color" />
</declare-styleable>

自定义控件AttrDeclareView继承View,使用Context.obtainStyledAttributes(AttributeSet, int[])方法来解析自定义属性,得到自定义属性background_color的值,调用TypedArray.recycle()方法释放资源,最后设置背景色。

public class AttrDeclareView extends View {

    public AttrDeclareView(Context context) {
        this(context, null);
    }

    public AttrDeclareView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AttrDeclareView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AttrDeclareView);
        int color = a.getColor(R.styleable.AttrDeclareView_background_color, 0);
        a.recycle();

        if (color != 0) {
            setBackgroundColor(color);
        }
    }

}

布局文件分别引用不同的背景色值。添加命名空间xmlns:app="http://schemas.android.com/apk/res-auto"。引用自定义属性时,使用命名空间+属性名的方式。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.blog.demo.custom.widget.AttrDeclareView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:background_color="#ff00ff00"/>
    <com.blog.demo.custom.widget.AttrDeclareView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:background_color="#ffff8800" />
    <com.blog.demo.custom.widget.AttrDeclareView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:background_color="@color/red" />
</LinearLayout>

效果如下
在这里插入图片描述

2. 不同控件使用同一属性

不同控件使用相同的自定义属性名时,两者会有冲突,需要将属性名提取到外面进行声明。

<attr name="indicator_color" format="color" />

<declare-styleable name="AttrDeclareView">
    <attr name="background_color" format="color" />
    <attr name="indicator_color" />
</declare-styleable>

3. 使用系统属性

attrs.xml文件,我们需要在AttrDeclareView中需要设置字符,同时设置字符的颜色和字体大小。

<declare-styleable name="AttrDeclareView">
    <attr name="background_color" format="color" />
    <attr name="indicator_color" />
    <attr name="android:text" format="string" />
    <attr name="android:textSize" format="dimension" />
    <attr name="android:textColor" format="color" />
</declare-styleable>

自定义控件AttrDeclareView,获取自定义文本数据,并在onDraw(Canvas)方法中调用。

public class AttrDeclareView extends View {

    private String mText;
    private Paint mPaint;

    public AttrDeclareView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AttrDeclareView);
        int color = a.getColor(R.styleable.AttrDeclareView_background_color, 0);
        mText = a.getString(R.styleable.AttrDeclareView_android_text);
        int textSize = a.getDimensionPixelSize(R.styleable.AttrDeclareView_android_textSize, 0);
        int textColor = a.getColor(R.styleable.AttrDeclareView_android_textColor, 0);
        a.recycle();

        if (color != 0) {
            setBackgroundColor(color);
        }

        if (mText != null) {
            mPaint = new Paint();
            mPaint.setColor(textColor);
            mPaint.setTextSize(textSize);
        }
    }

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

        if (mText != null) {
            canvas.drawText(mText, 0, getHeight()/2, mPaint);
        }
    }

}

布局文件

<com.blog.demo.custom.widget.AttrDeclareView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="This is a string"
    android:textSize="12sp"
    android:textColor="#FF000000"
    app:background_color="#ff0000ff"/>

效果如下
在这里插入图片描述

相关文章
Android 自定义控件属性
Android 自定义控件属性format详解
Android 自定义控件属性赋值

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值