精通Android自定义View(四)自定义属性使用详解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zl18603543572/article/details/88369714

1、简述

对于自定义属性,遵循以下几步,就可以实现:

  • 自定义一个CustomView(extends View )类
  • 编写values/attrs.xml,在其中编写styleable和item等标签元素
  • 在布局文件中CustomView使用自定义的属性(注意namespace)
  • 在CustomView的构造方法中通过TypedArray获取

2、自定义一个View

2.1 定义View
public class CustomView extends View {

    //构造函数会在new的时候调用
    public CustomView(Context context) {
        this(context, null);
    }
    //在布局中使用
    public CustomView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
    //布局layout中调用,但是会有style
    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
  }
2.2 编写values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomView">
        <attr name="text" format="string" />
        <attr name="test2" format="integer" />
    </declare-styleable>

</resources>
2.3 在布局文件中CustomView使用自定义的属性
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    
    <com.studyyoun.customviewapplication.CustomView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:text="1234" />

</LinearLayout>
2.4 在CustomView的构造方法中通过TypedArray获取
    //在布局中使用
    public CustomView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);

        //获取自定义View中自定义属性的值
        TypedArray lTypedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
        String text = lTypedArray.getString(R.styleable.CustomView_text);
    }

3、AttributeSet与TypedArray

  • AttributeSet可以获得布局文件中定义的所有属性的key和value
        //获取属性的数量
        int count = attrs.getAttributeCount();
        for (int i = 0; i < count; i++) {
            //属性名称
            String attrName = attrs.getAttributeName(i);
            //属性值
            String attrVal = attrs.getAttributeValue(i);
            Log.e("CustomView", "attrName = " + attrName + " , attrVal = " + attrVal);
        }

  • TypedArray其实是用来简化我们的工作的,比如上例,如果布局中的属性的值是引用类型(比如:@dimen/dp100),如果使用AttributeSet去获得最终的像素值,那么需要第一步拿到id,第二步再去解析id。而TypedArray正是帮我们简化了这个过程

4、declare-styleable标签

  • declare-styleable标签 中的name属性定义的是这组自定义属性组的名称,也就是说在自定义控件中引用自定义属性时所有用的标识符
如这里定义的 为 CustomView
<declare-styleable name="CustomView">
       ... ...
</declare-styleable>

在引用的时候为 
 TypedArray lTypedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView);

5、定义可被多个自定义控件使用的属性

将属性定义在标签之外,这样自定义属性就可以被多个自定义控件使用了

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

    <!-- 将公用的属性定义在<declare-styleable></declare-styleable>标签之外,即可被多个自定义控件使用 -->
    <attr name="pubbasecolor" format="color" />

    <declare-styleable name="test">
        <attr name="text" format="string" />
        <attr name="testAttr" format="integer" />
    </declare-styleable>

</resources>

猜你喜欢

转载自blog.csdn.net/zl18603543572/article/details/88369714