android自定義屬性

                                 android自定義屬性

一、引言

對於自定義屬性,你們確定都不陌生,遵循如下幾步,就能夠實現:java

  1. 自定義一個CustomView(extends View )類
  2. 編寫values/attrs.xml,在其中編寫styleable和item等標籤元素
  3. 在佈局文件中CustomView使用自定義的屬性(注意namespace)
  4. 在CustomView的構造方法中經過TypedArray獲取

那麼,我有幾個問題:android

  • 以上步驟是如何奏效的?
  • styleable 的含義是什麼?能夠不寫嘛?我自定義屬性,我聲明屬性就行了,爲何必定要寫個styleable呢?
  • 若是系統中已經有了語義比較明確的屬性,我能夠直接使用嘛?
  • 構造方法中的有個參數叫作AttributeSet 
    (eg: MyTextView(Context context, AttributeSet attrs) )這個參數看名字就知道包含的是參數的數組,那麼我能不能經過它去獲取個人自定義屬性呢?
  • TypedArray是什麼鬼?從哪冒出來的,就要我去使用?

二、常見的例子

接下來經過例子來回答上述問題,問題的回答順序不定~~你們先看一個常見的例子,即上述幾個步驟的代碼化。數組

  • 自定義屬性的聲明文件attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Test">
        <attr name="text" format="string"/>
        <attr name="textAttr" format="integer"/>
    </declare-styleable>
</resources>
  • 自定義View類
package qu.com.handlerthread;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by quguangle on 2016/11/25.
 */

public class MyTextView extends View {
    private static final String TAG = MyTextView.class.getSimpleName();
    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.Test);
        String text = typedArray.getString(R.styleable.Test_text);
        int textAttr = typedArray.getInteger(R.styleable.Test_textAttr,-1);

        Log.e(TAG,"text:"+text+"-----"+"textAttr:"+textAttr);
        typedArray.recycle();
    }

    public MyTextView(Context context) {
        super(context);
    }
}
  • 佈局文件中使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:qgl="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mMyLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <qu.com.handlerthread.MyTextView
        android:id="@+id/btnTest"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        qgl:text="Hello World"
        qgl:textAttr="250"
       />
</LinearLayout>

運行結果後,打印記錄爲:佈局

應該都不意外吧,注意下,個人styleable的name寫的是test,因此說這裏並不要求必定是自定義View的名字。spa

三、AttributeSet與TypedArray

下面考慮:.net

構造方法中的有個參數叫作AttributeSet(eg: MyTextView(Context context, AttributeSet attrs) )這個參數看名字就知道包含的是參數的集合,那麼我能不能經過它去獲取個人自定義屬性呢?code

首先AttributeSet中的確保存的是該View聲明的全部的屬性,而且外面的確能夠經過它去獲取(自定義的)屬性,怎麼作呢? 
其實看下AttributeSet的方法就明白了,下面看代碼。orm

public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        int count = attrs.getAttributeCount();
        for (int i = 0; i < count; i++) {
            String attrName = attrs.getAttributeName(i);
            String attrVal = attrs.getAttributeValue(i);
            Log.e(TAG, "attrName = " + attrName + " , attrVal = " + attrVal);
        }

        // ==>use typedarray ...

    }

打印結果:xml

結合上面的佈局文件,你發現了什麼? 
我擦,果真很神奇,真的得到全部的屬性,恩,沒錯,經過AttributeSet能夠得到佈局文件中定義的全部屬性的key和value(還有一些方法,本身去嘗試),那麼是否是說TypedArray這個鬼能夠拋棄了呢?答案是:NO!utf-8

如今關注下一個問題:

TypedArray是什麼鬼?從哪冒出來的,就要我去使用?

咱們簡單修改下,佈局文件中的MyTextView的屬性。

<qu.com.handlerthread.MyTextView
    android:id="@+id/btnTest"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    qgl:text="@string/testView"
    qgl:textAttr="250"
    />
</LinearLayout>

如今再次運行的結果是:

發現了什麼?經過AttributeSet獲取的值,若是是引用都變成了@+數字的字符串。你說,這玩意你能看懂麼?那麼你看看最後一行使用TypedArray獲取的值,是否是瞬間明白了什麼。

TypedArray實際上是用來簡化咱們的工做的,好比上例,若是佈局中的屬性的值是引用類型(好比:@dimen/dp100),若是使用AttributeSet去得到最終的像素值,那麼須要第一步拿到id,第二步再去解析id。而TypedArray正是幫咱們簡化了這個過程。

貼一下:若是經過AttributeSet獲取最終的像素值的過程:

int widthDimensionId =  attrs.getAttributeResourceValue(0, -1);
        Log.e(TAG, "layout_width= "+getResources().getDimension(widthDimensionId));

ok,如今別人問你TypedArray存在的意義,你就能夠告訴他了。

四、declare-styleable

咱們已經解決了兩個問題,接下來,咱們看看佈局文件,咱們有一個屬性叫作:zhy:text。 
總所周知,系統提供了一個屬性叫作:Android:text,那麼我以爲直接使用android:text更nice,這樣的話,考慮問題:

若是系統中已經有了語義比較明確的屬性,我能夠直接使用嘛?

答案是能夠的,怎麼作呢? 
直接在attrs.xml中使用android:text屬性。

<declare-styleable name="test">
        <attr name="android:text" />
        <attr name="testAttr" format="integer" />
    </declare-styleable>

注意,這裏咱們是使用已經定義好的屬性,不須要去添加format屬性(注意聲明和使用的區別,差異就是有沒有format)。 
而後在類中這麼獲取:ta.getString(R.styleable.test_android_text);佈局文件中直接android:text="@string/hello_world"便可。

這裏提一下,系統中定義的屬性,其實和咱們自定義屬性的方式相似,你能夠在sdk/platforms/android-xx/data/res/values該目錄下看到系統中定義的屬性。而後你能夠在系統提供的View(eg:TextView)的構造方法中發現TypedArray獲取屬性的代碼(本身去看一下)。

ok,接下來,我在想,既然declare-styleable這個標籤的name都能隨便寫,這麼隨意的話,那麼考慮問題:

styleable 的含義是什麼?能夠不寫嘛?我自定義屬性,我聲明屬性就行了,爲何必定要寫個styleable呢?

其實的確是能夠不寫的,怎麼作呢?

  • 首先刪除declare-styleable的標籤

那麼如今的attrs.xml爲:

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <attr name="text" format="string"/>
        <attr name="textAttr" format="integer"/>
</resources>

MyTextView實現

**
 * Created by quguangle on 2016/11/25.
 */

public class MyTextView extends View {
    private static final String TAG = MyTextView.class.getSimpleName();
    private static final int[] mAttr = { R.attr.textAttr,R.attr.Mytext};
    private static final int ATTR_ANDROID_TEXT = 0;
    private static final int ATTR_TESTATTR = 1;

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        // ==>use typedarray
        TypedArray ta = context.obtainStyledAttributes(attrs, mAttr);

        String text = ta.getString(ATTR_ANDROID_TEXT);
        int textAttr = ta.getInteger(ATTR_TESTATTR, -1);
        //輸出 text = Hello world! , textAttr = 520
        Log.e(TAG, "text = " + text + " , textAttr = " + textAttr);

        ta.recycle();
    }
}

貌似多了些代碼,能夠看到咱們聲明瞭一個int數組,數組中的元素就是咱們想要獲取的attr的id。而且咱們根據元素的在數組中的位置,定義了一些整形的常量表明其下標,而後經過TypedArray進行獲取。 
能夠看到,咱們本來的:

R.styleable.test => mAttr
R.styleable.test_text => ATTR_ANDROID_TEXT(0)
R.styleable.test_testAttr => ATTR_TESTATTR(1)

打印結果: 

11-25 13:37:16.086 29383-29383/qu.com.handlerthread E/MyTextView: text = 250 , textAttr = -1

那麼其實呢?android在其內部也會這麼作,按照傳統的寫法,它會在 R.java生成以下代碼:

public static final class attr {
    public static final int testAttr=0x7f0100a9;
    }
public static final class styleable {
     public static final int test_android_text = 0;
     public static final int test_testAttr = 1;
      public static final int[] test = {
            0x0101014f, 0x7f0100a9
        };
    }

ok,根據上述你應該發現了什麼。styleale的出現系統能夠爲咱們完成不少常量(int[]數組,下標常量)等的編寫,簡化咱們的開發工做(想一想若是一堆屬性,本身編寫常量,你得寫成什麼樣的代碼)。那麼你們確定還知道declare-styleable的name屬性,通常狀況下寫的都是咱們自定義View的類名。主要爲了直觀的表達,該declare-styleable的屬性,都是改View所用的。

ok,如今5個問題,回答了4個,第一個問題:

自定義屬性的幾個步驟是如何奏效的?

恩,上述以及基本涵蓋了這個問題的答案,你們本身總結,因此:略。

總結下今天的博客。

  • attrs.xml裏面的declare-styleable以及item,android會根據其在R.java中生成一些常量方便咱們使用(aapt乾的),本質上,咱們能夠不聲明declare-styleable僅僅聲明所需的屬性便可。
  • 咱們在View的構造方法中,能夠經過AttributeSet去得到自定義屬性的值,可是比較麻煩,而TypedArray能夠很方便的便於咱們去獲取。
  • 咱們在自定義View的時候,可使用系統已經定義的屬性。
相關文章
相關標籤/搜索