Android 自定义view时用到的TypedArray

前言:

在自定义控件的时候,如果我们想额外的添加一些属性,就会用到TypedArray这个类,那么这个类是怎么得到的,以及怎么使用的,这里作个简单介绍。

创建自定义属性

首先创建values\attrs.xml,在attrs.xml中声明自定义属性:

<declare-styleable name="MyFirstCustomerView">
    <attr name="text" format="string" />
    <attr name="textColor" format="color"/>
    <attr name="textSize" format="dimension"/>
</declare-styleable>
  • 自定义string类型,属性名为text
  • 自定义color类型,属性名为textColor
  • 自定义dimension类型,属性名为textSize

declare-styleable这个标签的作用其实就是可以为我们完成很多常量(int[]数组,下标常量)等的编写,简化我们的开发工作

format还有如下类型:

format介绍
reference表示引用,参考某一资源ID
string表示字符串
color表示颜色值
dimension表示尺寸值
boolean表示布尔值
integer表示整型值
float表示浮点值
fraction表示百分数
enum表示枚举值
flag表示位运算

自定义空间在xml里面布局的时候引用自定义的属性。

CustomView是自定义控件,引用了app:text、app:textColor、 app:textSize="18sp"三个自定义属性

<com.ligj.test.view.CustomView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:text="这是自定义属性"
    app:textColor="@android:color/black"
    app:textSize="18sp"/>

当然,起作用的前提是,要在自定义的view里面处理:

public class CustomView extends View {

public Paint paint;
private String text = "";//文本内容
private int textColor = 0xDD333333;  //字体颜色
private float textSize = 20;//字体大小设置

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

public CustomView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(attrs);
}

private void init(AttributeSet attrs) {
    TypedArray a = getResources().obtainAttributes(attrs, R.styleable.MyFirstCustomerView);//获取TypedArray
    textColor = a.getColor(R.styleable.CustomerView_textColor, textColor);//获取布局中设置的自定以颜色
    textSize = a.getDimension(R.styleable.CustomerView_textSize, textSize);//获取布局中设置的自定义字体大小
    text = a.getString(R.styleable.CustomerView_text);//获取布局中设置的自定义文本
    paint = new Paint();//初始化 画笔
    paint.setTextSize(textSize);//画笔字体大小设置
    paint.setColor(textColor);//画笔的颜色
    paint.setStyle(Paint.Style.FILL);//画笔风格
    a.recycle();//切记:在使用TypedArray后需要回收
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawText(text, 100, 100, paint);
}}

-TypedArray用完一定要回收。其实obtainAttributes()底层是native方法,也就是用C++实现的,他会提取自定义控件属性的的值保存TypedArray中的mData数组中,这个数组的大小是由你定义控件属性的个数决定的,是它的6倍,上面的attrs其实就是你自定义属性的个数。

Context当前上下文
AttributeSet为xml里一个节点下面的属性的集合,这个类一般都是系统在生成有xml配置的组件时生成,可以理解为当前自定义控件下的所有属性集合;提供TypedArray检索的范围
defStyleAttr

在当前包含了一个引用到为TypedArray提供默认值的样式资源的theme中的一种属性。可以为0,但是为0的时候就不会再去寻找默认的;提供TypedArray检索的范围(注:这里的默认也就是defStyleRes)

总结

  • declare-styleable标签可以为我们完成很多常量(int[]数组,下标常量)等的编写,简化我们的开发工作,可以不声明,但是需要在自定义控件中,声明引用自定义属性数组
  • TypedArray 可有obtainAttributes()、obtainStyledAttributes()方法创建,常用obtainStyledAttributes(int resid, int[] attrs)构造方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值