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 16:43
下一篇 2024-10-19 16:43

相关推荐

  • Linux sync详解

    一、sync概述 sync是Linux中一个非常重要的命令,它可以将文件系统缓存中的内容,强制写入磁盘中。在执行sync之前,所有的文件系统更新将不会立即写入磁盘,而是先缓存在内存…

    编程 2025-04-25
  • 神经网络代码详解

    神经网络作为一种人工智能技术,被广泛应用于语音识别、图像识别、自然语言处理等领域。而神经网络的模型编写,离不开代码。本文将从多个方面详细阐述神经网络模型编写的代码技术。 一、神经网…

    编程 2025-04-25
  • nginx与apache应用开发详解

    一、概述 nginx和apache都是常见的web服务器。nginx是一个高性能的反向代理web服务器,将负载均衡和缓存集成在了一起,可以动静分离。apache是一个可扩展的web…

    编程 2025-04-25
  • 详解eclipse设置

    一、安装与基础设置 1、下载eclipse并进行安装。 2、打开eclipse,选择对应的工作空间路径。 File -> Switch Workspace -> [选择…

    编程 2025-04-25
  • Python输入输出详解

    一、文件读写 Python中文件的读写操作是必不可少的基本技能之一。读写文件分别使用open()函数中的’r’和’w’参数,读取文件…

    编程 2025-04-25
  • MPU6050工作原理详解

    一、什么是MPU6050 MPU6050是一种六轴惯性传感器,能够同时测量加速度和角速度。它由三个传感器组成:一个三轴加速度计和一个三轴陀螺仪。这个组合提供了非常精细的姿态解算,其…

    编程 2025-04-25
  • git config user.name的详解

    一、为什么要使用git config user.name? git是一个非常流行的分布式版本控制系统,很多程序员都会用到它。在使用git commit提交代码时,需要记录commi…

    编程 2025-04-25
  • Python安装OS库详解

    一、OS简介 OS库是Python标准库的一部分,它提供了跨平台的操作系统功能,使得Python可以进行文件操作、进程管理、环境变量读取等系统级操作。 OS库中包含了大量的文件和目…

    编程 2025-04-25
  • Java BigDecimal 精度详解

    一、基础概念 Java BigDecimal 是一个用于高精度计算的类。普通的 double 或 float 类型只能精确表示有限的数字,而对于需要高精度计算的场景,BigDeci…

    编程 2025-04-25
  • Linux修改文件名命令详解

    在Linux系统中,修改文件名是一个很常见的操作。Linux提供了多种方式来修改文件名,这篇文章将介绍Linux修改文件名的详细操作。 一、mv命令 mv命令是Linux下的常用命…

    编程 2025-04-25

发表回复

登录后才能评论