Android控件:自定义属性

当我们自定义View时,有时候需要提供一些自定义的参数,怎样来提供这些自定义的属性呢?步骤如下:

1、在res/values下建立一个attrs.xml文件;
2、在自定义View构造函数里面获取这些属性;
3、在布局文件中指定命名空间以及填写属性。


新建attrs.xml文件

res/values/attrs.xml,使用到declare-styleable标签,该标签的内容是attr标签,有nameformat两个属性。name顾名思义就是自定义属性的名字,而format则有string、color、boolean、dimension、integer、float、fraction等值。

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

    <declare-styleable name="MyView">
        <attr name="myViewText" format="string"></attr>
        <attr name="isDigits" format="boolean"></attr>
    </declare-styleable>

</resources>  

构造函数获取该属性

通过TypeArray获取到自定义的属性,然后获取布局文件中的值。

public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyView, defStyleAttr, 0);
    String str = ta.getString(R.styleable.MyView_myViewText);
    boolean isDigits = ta.getBoolean(R.styleable.MyView_isDigits, false);
    ta.recycle();
    Log.e("TAG", str + " " + isDigits);
}

当我们没有在使用TypedArray后调用recycle,编译器会提示“This TypedArray should be recycled after use with #recycle()”。官方的解释是:回收TypedArray,以便后面重用。
当我们不做是否有此属性的判断就获取一个为null的String对象,操作时就会有空指针异常。除了上面这种写法以外还有常见的另外一种,这种写法比上面的写法安全一点。

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyView, defStyleAttr, 0);
// 布局文件中使用自定义属性的个数
int count = ta.getIndexCount();
for (int i = 0; i < count; i++) {
    int index = ta.getIndex(i);
    // 判断出有该属性时才做操作
    switch (index) {
        case R.styleable.MyView_myViewText:
            String str = ta.getString(index);
            Log.e("TAG", count + " " + str);
            break;
        case R.styleable.MyView_isDigits:
            boolean bool = ta.getBoolean(index, false);
            Log.e("TAG", count + " " + bool);
            break;
    }
}
ta.recycle();

本例只是简单打印出属性值,真正的自定义View需要根据这些属性值影响绘制流程。


布局文件中使用

指定命名空间:

xmlns:custom="http://schemas.android.com/apk/res-auto"

注意:使用gradle构建一律使用上面的格式,而如果是Eclipse工程最后apk/则指定包名。

使用属性:

custom:isDigits="false"
custom:myViewText="hello world!" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.leelit.example.customview.MyView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        custom:isDigits="false"
        custom:myViewText="hello world!" />

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值