[Android 自定義 View] 自定義屬性你真的理解嗎?

版權聲明:禁止一切商業行爲,轉載請註明出處 mp.csdn.net/mdeditor/99… 做者:代聖達

在這裏插入圖片描述
最近本人整理了一些本身的學習筆記,內容比較零散不適合發表在博客上,現託管在 GitHub 上歡迎批評指正

@[toc]php

自定義屬性其實就是一些 xml 標籤,他們經過 xml 文件的形式,能夠配置某些 View 的信息,讓自定義 View 使用起來更加靈活。java

想必不少同窗都已經對於自定義屬性使用的駕輕就熟了,可是有一些細節你真的知道嗎?好比 AttributeSet、TypedArray 、declare-styleable 這些類和標籤的內容你都清楚嗎,在獲取自定義屬性的時候爲何要用android

Context.obtainStyledAttributes(AttributeSet, R.styleable.XXXX);
複製代碼

方法呢?全部的答案都會在這篇文章裏。git

必須是 res/values/attrs.xml嗎?

不少文章都說:須要在 res/values 目錄下建立 attrs.xml 文件而後在裏面寫咱們須要的屬性,其實這是不太準確的,經過實驗證實,文件的名字能夠隨意指定,不必定必須是 attrs.xml !github

\[外鏈圖片轉存失敗(img-MF8eTbMt-1565511612610)(assets/image-20190809010946000.png)\]

例如筆者自定義了一個 custom.xml 文件, 裏面的內容符合自定義屬性的規範,在 View 中也是能夠正常訪問到的。(具體緣由尚不清楚,多是 Android Stuido 的功能)app

文件結構

文件結構

  1. name space : 命名空間,名字能夠隨便起,可是最好和自定義 View 的名字相同,由於 Android Stuido 能夠幫咱們作一些事情,好比說 command + 手錶左鍵,能夠直接跳轉佈局

  2. attr name :這就是咱們自定義屬性的名字,具體的格式仍是模仿 android 內部的方式,駝峯式命名或者是 名稱_名稱學習

  3. format : 屬性的具體類型,此處講解一些特殊的類型,此處不是重點,網上文章不少。ui

    a .reference: 資源idthis

    <ImageView android:background = "@drawable/圖片ID"/>
    複製代碼

    b. fraction : 百分數

    • 屬性定義
    <attr name="pivotX" format="fraction"/>
    複製代碼
    • 使用
    <android:pivotX = "200%"/>
    複製代碼

    c. flag : 位運算,能夠在使用過程當中指定多個值

    • 定義
    <attr name="gravity" format="flags">
    	<flag name="top" value="0x30"/>
    	<flag name="bottom" value="0x50" />
    	<flag name="left" value="0x03" />
    	<flag name="right" value="0x05" />
    	<flag name="center_vertical" value="0x10" />
    </attr>
    複製代碼
    • 使用
    <TextView android:gravity="bottom|left"/>
    複製代碼

    d. enum : 枚舉

    • 屬性定義
    <attr name="orientation" format="enum">
    	<enum name="horizontal" value="0"/>
    	<enum name="vertical" value="1"/>
    </attr>
    複製代碼

    e. 混合模式 :指定屬性的時候能夠指定多種類型值

    <attr name="background" format="reference|color"/>
    複製代碼

屬性的使用

  1. 定義屬性
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomAttrsDemo">
        <attr name="text_color" format="color" />
        <attr name="text" format="dimension" />
    </declare-styleable>

</resources>
複製代碼
  1. 在 xml 文件中使用

    在佈局文件中使用,首先須要引入命名空間,這樣才能找到咱們包中的 attrs,這裏咱們引入了命名空間 app,res-auto 表示自動查找

xmlns:app="http://schemas.android.com/apk/res-auto"
複製代碼
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    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.example.dsd.demo.ui.draw.attrs.CustomAttrsDemo
        android:id="@+id/custom_attrs_demo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        app:text_color="#333333"
        app:text_size="10sp"/>

</FrameLayout>
複製代碼
  1. 在自定義 View 中使用 此處要注意的是 TypedArray 在使用後必定要調用 TypedArray.recycler() 方法進行回收,不然會內存泄漏。
/** * 自定義屬性 Demo * * Created by im_dsd on 2019-08-11 */
   public class CustomAttrsDemo extends android.support.v7.widget.AppCompatTextView {
   
       private final int mTextColor;
       private final int mTextSize;
   
       public CustomAttrsDemo(Context context, AttributeSet attrs) {
           super(context, attrs);
           TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomAttrsDemo);
           mTextColor = array.getColor(R.styleable.CustomAttrsDemo_textColor, Color.BLACK);
           mTextSize = array.getDimensionPixelSize(R.styleable.CustomAttrsDemo_textSize, 18);
           // 注意使用完成以後必定要回收
           array.recycle();
       }
   }
複製代碼

AttributeSet、TypedArray 、declare-styleable

AttributeSet

A collection of attributes, as found associated with a tag in an XML document. Often you will not want to use this interface directly, instead passing it to {@link android.content.res.Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int) Resources.Theme.obtainStyledAttributes()}

能夠看到 AtttirbuteSet 是一個大的屬性集合,裝載了此 View 全部的屬性,用戶能夠經過方法:

Context.obtainStyledAttributes(AttributeSet, R.styleable.XXXX);
複製代碼

獲取指定的屬性集合(一個明確的小集合 TypedArray)

TypedArray

TypedArray array = Context.obtainStyledAttributes(AttributeSet, R.styleable.XXXX);
複製代碼

TypedArray裏面裝的就是具體的屬性了,咱們能夠經過 :array.getXXXX 的方法獲取具體的屬性值

注意: 在使用後必定要調用array.recycle 用於釋放內存空間,否則此內存空間就被浪費了

declare-styleable

此標籤的做用就是將屬性分組,在 Context.obtainStyledAttributes 方法中指定須要加載的屬性組

此次自定義屬性就完成了。

總結

自定義屬性仍是很簡單的,可是不少同窗都把加載自定義屬性的過程當錯了模版代碼背了下來,不明白的其中的道理,在使用過程當中仍是很難達到靈活運用。

  1. 總的來講,自定義屬性就是爲了讓自定義 View 在 xml 文件中更加靈活,讓更多的屬性能夠被使用者控制。
  2. 自定義的時候咱們須要在 res/values 文件夾下建立 attrs.xml 文件(文件名稱不是固定的,能夠隨意起可是仍是叫 attrs.xml 統一使用習慣的較好)而後在文件中使用 declare-styleable 標籤自定義一個屬性組。
  3. 在 layout.xml 文件中使用的時候須要引入命名空間
  4. 在具體的自定義 View 使用中,須要使用方法
TypeArray array = Context.obtainStyledAttributes(AttributeSet, R.styleable.屬性集合名);
複製代碼

獲取指定的屬性集 R.styleable.屬性集合名,而後經過方法:

array.getXXXX(R.styleable.屬性集合名_屬性名, 默認值)的方式獲取屬性
array.recyler()
複製代碼

方法獲取屬性值


版權聲明:禁止一切商業行爲,轉載請註明出處 mp.csdn.net/mdeditor/99… 做者:代聖達

相關文章
相關標籤/搜索