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/zh-hant/n/143427.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
WFQO的頭像WFQO
上一篇 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

發表回復

登錄後才能評論