Android自定义View(三)自定义属性AttributeSet

Android自定义View(三)自定义属性AttributeSet自定义View的时候通常需要提供一些自定义属性,自定义属性非常简单,只需要在res资源目录的values目录下创建一个attrs.xml的属性定义文件,然后在该文件中定义相应的属性,并在自定义View的构造函数中获取并设置自定义属性的默认值即可。假设在attrs.xml中自定义如下属性:<declare-styleablename=”Custo…

       自定义View的时候通常需要提供一些自定义属性,自定义属性非常简单,只需要在res资源目录的values目录下创建一个attrs.xml的属性定义文件,然后在该文件中定义相应的属性,并在自定义View的构造函数中获取并设置自定义属性的默认值即可。

       假设在attrs.xml中自定义如下属性:

    <declare-styleable name="CustomView">
        <attr name="property" format="String"/>
    </declare-styleable>

        declare-styleable标签中的name代表自定义View的类名,而attr标签中的name表示自定义属性的名称,format表示自定义属性的类型,format的取值如下图所示:

Android自定义View(三)自定义属性AttributeSet

        这里将一一介绍自定义属性的每种类型以及如何在自定义View的构造函数中获取该类型的属性:

一、string类型

        1、在attrs.xml中定义属性为string类型:

    <attr name="property" format="String"/>

        2、相应的在自定义View的构造函数中可以通过如下方式获取:

    private String property;
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
        property = ta.getString(R.styleable.CustomView_property);
    }

              首先通过Context的obtainStyledAttributes方法获取TypedArray,再通过getString获取自定义属性property;

        3、然后就可以在布局文件中设置自定义属性了。

    <com.exam.customview.CustomView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:property="test" />

  二、boolean类型

        1、在attrs.xml中定义属性为boolean类型:

    <attr name="property" format="boolean" />

        2、在自定义View的构造函数中通过如下方式获取:

    private boolean property;
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
    property = ta.getBoolean(R.styleable.CustomView_property, false);

              通过TypedArray的getBoolean方法获取自定义属性,同时设置属性的默认值。

        3、在布局文件中设置自定义属性:

Android自定义View(三)自定义属性AttributeSet

三、dimension类型

        1、在attrs.xml中定义属性为dimension类型,即设置属性为尺寸类型:

    <attr name="property" format="dimension" />

        2、在自定义View的构造函数中通过如下方式获取:

    private float property;
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
    property = ta.getDimension(R.styleable.CustomView_property, 10.0f);

             通过TypedArray的getDimension方法获取自定义属性,同时设置属性的默认值。

        3、在布局文件中设置自定义属性:

    app:property="1dp"

四、float类型

        1、在attrs.xml中定义属性为float类型:

    <attr name="property" format="float" />

        2、在自定义View的构造函数中通过如下方式获取:

    private float property;
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
    property = ta.getFloat(R.styleable.CustomView_property, 0.0f);

             通过TypedArray的getFloat方法获取自定义属性,同时设置属性的默认值。

        3、在布局文件中设置自定义属性:

    app:property="0.8" 

五、integer类型

        1、在attrs.xml中定义属性为integer类型:

    <attr name="property" format="integer" />

        2、在自定义View的构造函数中通过如下方式获取:

    private int property;
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
    property = ta.getInt(R.styleable.CustomView_property, 0);

             通过TypedArray的getInt方法获取自定义属性,同时设置属性的默认值。

        3、在布局文件中设置自定义属性:

    app:property="10"

六、enum类型

        1、在attrs.xml中定义属性为enum类型,即设置属性为枚举类型,枚举类型需要通过enum标签指定枚举的常量,假设自定义一个属性location,它有五个常量:left、right、top、bottom、center,其在attrs.xml中的结构如下:

    <attr name="location" format="enum">
        <enum name="left" value="0" />
        <enum name="right" value="1" />
        <enum name="top" value="2" />
        <enum name="bottom" value="3" />
        <enum name="center" value="4" />
    </attr>

        enum标签中name代表枚举的常量,值得注意的是,value指的是枚举常量对应的值,因为TypedArray中并不存在类似的getEnum方法来获取枚举类型的属性,所以在获取枚举类型的属性时是通过value值来获取并设置默认值,比如可以指定value为Int类型然后通过TypedArray的getInt获取。

        2、在自定义View的构造函数中通过如下方式获取:

    private int location;
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
    location = ta.getInt(R.styleable.CustomView_location, 0);

             通过TypedArray的getInt方法获取枚举类型的属性,同时设置属性的默认值,这里的默认值为枚举常量对应的value值。

        3、在布局文件中设置自定义属性:

Android自定义View(三)自定义属性AttributeSet

七、color类型

        1、在attrs.xml中定义属性为color类型:

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

        2、在自定义View的构造函数中通过如下方式获取:

    private int property;
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
    property = ta.getColor(R.styleable.CustomView_property, Color.parseColor("#FFFFFF"));

             通过TypedArray的getColor方法获取自定义属性,同时设置属性的默认值。

        3、在布局文件中设置自定义属性:

    app:property="#000000"

八、reference类型

        在attrs.xml中定义属性为reference类型,reference表示一个资源id,通常可以用来引用一张图片(drawable)或字符串数组(string-array):

    <attr name="property" format="reference" />

        当用来引用一张图片或Drawable类型的资源(如ShapeDrawable)时,在自定义View的构造函数中需要将属性设置为Drawable类型,并通过TypedArray的getDrawable方法获取:

private Drawable property;
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
property = ta.getDrawable(R.styleable.CustomView_property);

        在布局文件中设置自定义属性:

app:property="@mipmap/demo"

        当用来引用字符串数组时,在自定义View的构造函数中需要将属性设置为CharSequence[]数组类型,并通过TypedArray的getTextArray方法获取:

private CharSequence[] property;
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
property = ta.getTextArray(R.styleable.CustomView_property);

        在布局文件中设置自定义属性:

app:property="@array/item"

九、flags类型

        flags类型用于进行位或运算,可以指定并存的属性值,即支持app:property=”option1|option2″的格式声明,app:property属性的取值既可以是option1或者是option2。举个例子,在Android开发中为了避免屏幕旋转导致Activity重新创建,可以通过指定android:configChanges=”orientation|keyboardHidden|screenSize”来实现。

        这个属性的用法和enum类型是有些许相似的地方,主要区别在于flags类型支持指定有限的几个属性值中的一个值或多个值并存的形式,但是enum类型只能指定有限的几个属性值中的一个取值。它的使用方法如下:

        1、在attrs.xml中定义属性为flags类型:

<attr name="property" format="flags">
    <flag name="option1" value="0" />
    <flag name="option2" value="1" />
    <flag name="option3" value="2" />
    <flag name="option4" value="3" />
    <flag name="option5" value="4" />
</attr>

        2、在自定义View的构造函数中通过如下方式获取:

private int property;
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
property = ta.getInt(R.styleable.CustomView_property, 0);

        3、在布局文件中设置自定义属性:

app:property="option1|option3|option5"

十、fraction类型

        1、在attrs.xml中定义属性为fraction类型,即设置属性为百分比数值类型:

<attr name="property" format="fraction" />

        2、在自定义View的构造函数中通过如下方式获取:

private float property;
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
property = ta.getFraction(R.styleable.CustomView_property, 1, 1, 0);

        3、在布局文件中设置自定义属性:

app:property="30%p"
app:property="30%"

       如上所示,百分比属性支持两种形态的百分比声明:

       100% 表示相对于对象自身的百分比;

       100%p 表示相对于父容器的百分比;

       在构造函数中可以通过TypedArray的getFraction方法来获取百分比数值的属性,getFraction方法原型如下:

public float getFraction(@StyleableRes int index, int base, int pbase, float defValue)

      index:在attrs.xml中定义的资源id;

      base:该分数的基值,一般来说百分比属性对应的具体数值等于该分数乘以该基值,比如设置属性app:property=”30%“,设置基值base为1,则属性对应的具体值为相当于对象自身的30%,即0.3乘以1;而如果设置属性app:property=”30%”,设置基值为5的话,那么属性对应的具体值并不是对象自身的30%,而是0.3乘以5,即150%。为了更直观的展示该类型的属性,在构造函数中通过getFraction获取该类型的值时设置base为1即可;

       pbase:该分数的父基值,一般来说百分比属性对应的具体数值等于该分数乘以该父基值,比如设置属性app:property=”30%p“,设置基值pbase为1,则属性对应的具体值为相当于父容器的30%,即0.3乘以1,一般设为1即可;

       defValue:未设置属性时的默认值。

十一、组合类型

        1、比如需要设置背景既可以是颜色或者是一张图片,则可以在attrs.xml中定义如下:

<attr name="background_property" format="color|reference" />

        2、在自定义View的构造函数中通过如下方式获取:

private Drawable backgroundProperty;
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
backgroundProperty = ta.getDrawable(R.styleable.CustomView_background_property);

        在自定义VIew中可以通过instanceof区分是颜色还是图片类型的属性:

if (backgroundProperty instanceof ColorDrawable) {
    //执行相应操作
} else if (backgroundProperty instanceof BitmapDrawable) {
    //执行相应操作
}

        3、然后就可以在布局文件中设置自定义属性为颜色类型:

app:background_property="#000000"

             或者设置属性为图片类型:

app:background_property="@mipmap/demo"

  十二、系统属性

        1、有时候我们并不想自定义属性名称,而是想重用系统属性名称,比如自定义了一个控件,它有一个显示多少行的属性,我们知道系统有一个android:maxLines属性,那如何去重用这个属性呢?其实还是一样的,先在attrs.xml中定义属性:

<attr name="android:maxLines" format="integer" />

        2、在自定义View的构造函数中通过如下方式获取:

private int maxLines;
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
maxLines = ta.getInt(R.styleable.CustomView_android_maxLines, 1);

        3、然后就可以像TextView一样在布局文件中使用:

android:maxLines="1"

        下一节将介绍Android中Paint的常用方法

今天的文章Android自定义View(三)自定义属性AttributeSet分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/24086.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注