attributeset详解

一、attributeset概述

1、attributeset是Android中一个非常重要的类,它是View中的一个成员变量,用于存储所有的属性值。attributeset中包含了我们在xml中声明的所有属性,通过解析xml,系统将其绑定在View上。此外,attributeset还是自定义View中非常重要的一个类。

2、attributeset可以通过三种方式设置:

// 从资源中解析attributes
mContext.obtainStyledAttributes(attrs, R.styleable.MyView);
// 从主题中解析attributes
mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.MyView, 0, 0);
// 在java代码中直接新建attributes
AttributeSet as = new AttributeSet();

3、在编写自定义View时,我们需要重写view的三个构造方法,其中第一个和第二个方法都是通过调用第三个方法实现的。第三个方法中会传入一个attributeset类型的参数,这个attributeset对象保存了从xml文件中解析出来的属性值。

public class MyView extends View {
    public MyView(Context context) {
        this(context, null);
    }
    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

二、attributeset attrs详解

1、在attributeset中attrs是一个数组,包含了我们在xml文件中定义的所有属性。在自定义view中,我们可以通过使用TypedArray自定义获取每一个属性值。

TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.MyView);
int textColor = a.getColor(R.styleable.MyView_textColor, Color.BLACK);
float textSize = a.getDimension(R.styleable.MyView_textSize, 12);
String text = a.getString(R.styleable.MyView_text);

2、在xml文件中定义的属性可以分为以下三类:

(1)系统自带的属性,如android:layout_width,android:layout_height等;

(2)自定义的属性,在values/attrs.xml文件中定义,通过<declare-styleable>标签进行声明,<attr name=”xxxx” format=”yyyy”/>用来定义属性;

(3)继承自系统或其他库的属性。

3、在定义自定义属性时,可以通过format属性来定义属性值的类型,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView">
        <attr name="text" format="string" />
        <attr name="textSize" format="dimension" />
        <attr name="textColor" format="color" />
        <attr name="isBold" format="boolean" />
        <attr name="icon" format="reference" />
    </declare-styleable>
</resources>

三、attributeset类详解

1、attributeset类是一个接口,用于描述一个xml节点中的一组属性值。接口中包含了获取属性值的一系列方法。

public interface AttributeSet {
    int getAttributeCount();
    String getAttributeName(int index);
    String getAttributeValue(int index);
    String getAttributeValue(String namespace, String name);
    String getPositionDescription();
    int getAttributeNameResource(int index);
    int getAttributeListValue(String namespace, String attribute, String[] options, int defaultValue);
    boolean getAttributeBooleanValue(String namespace, String attribute, boolean defaultValue);
    int getAttributeResourceValue(String namespace, String attribute, int defaultValue);
    float getAttributeFloatValue(String namespace, String attribute, float defaultValue);
    int getAttributeIntValue(String namespace, String attribute, int defaultValue);
    int getAttributeUnsignedIntValue(String namespace, String attribute, int defaultValue);
    int getAttributeListValue(int index, String[] options, int defaultValue);
    boolean getAttributeBooleanValue(int index, boolean defaultValue);
    int getAttributeResourceValue(int index, int defaultValue);
    float getAttributeFloatValue(int index, float defaultValue);
    int getAttributeIntValue(int index, int defaultValue);
    int getAttributeUnsignedIntValue(int index, int defaultValue);
    String getIdAttribute();
}

2、在使用attributeset类时应该注意:

(1)getAttributeValue(String namespace, String name)在部分系统版本上存在问题,建议使用getAttributeValue(int index)

(2)getAttributeBooleanValue方法获取属性值是根据字符串值进行判断的,如果字符串值不是”true”或者”false”则会默认返回defaultValue。

四、总结

1、attributeset是android中非常重要的一个类,它用于存储View中的所有属性。一般情况下,我们不需要直接操作attributeset类,而是通过TypedArray类进行获取属性值。

2、在自定义View中,通过定义自定义属性可以使我们的View使用起来更加方便。

3、当使用attributeset类进行属性值获取时,需要注意一些细节问题。

4、在实际开发中,attributeset类会频繁使用,掌握其使用方法和特性是android开发者必备的技能之一。

原创文章,作者:WFQO,如若转载,请注明出处:https://www.506064.com/n/143427.html

(0)
WFQOWFQO
上一篇 2024-10-19
下一篇 2024-10-19

相关推荐

发表回复

登录后才能评论