android 自己定義控制項屬性(TypedArray以及attrs解釋)

來源:互聯網
上載者:User

標籤:gets   edit   size   bre   eva   標籤   enter   and   結束   

近期在搗鼓android 自己定義控制項屬性,學到了TypedArray以及attrs。在這當中看了一篇大神部落格Android 深入理解Android中的自己定義屬性。我就更加深入學習力一番。我就沿著這個學習,講一下流程吧,興許一篇還有應用。

1、attrs檔案編寫

<?xml version="1.0" encoding="utf-8"?><resources>    <attr name="titleText" format="string" />    <attr name="titleTextColor" format="color" />    <attr name="titleTextSize" format="dimension" />    <declare-styleable name="AuthCodeView">        <attr name="titleText" />        <attr name="titleTextColor" />        <attr name="titleTextSize" />    </declare-styleable></resources>
看到這上面的代碼有三個屬性,首先attr標籤是定義名字以及屬性。後面是一個declare-styleable組,這個組名字AuthCodeView,後面class中會用到。


2、在xml裡面怎麼引用以及使用。對照系統空間屬性

先看兩張圖。就瞭解大半了,也理解大半了。

a、自己定義屬性的名字的引用



b、細緻看圖上說明以及a跟b圖的比較。你就知道屬性名稱改變。以及怎麼引用。



怕上面圖片看不清。附上部分xml代碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:authcodeview="http://schemas.android.com/apk/res/com.example.authcodeview"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <com.example.authcodeview.view.AuthCodeView            android:id="@+id/AuthCodeView"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:padding="10dp"            authcodeview:titleText="3712"            authcodeview:titleTextColor="#00ffff"            authcodeview:titleTextSize="40sp" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="點擊驗證碼,換一張" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="輸入驗證碼" />        <EditText            android:id="@+id/editText1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:ems="10"            android:inputType="number" >            <requestFocus />        </EditText>    </LinearLayout>    <Button        android:id="@+id/button1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="驗證" /></LinearLayout>


重點看頭部layout中xmlns:android="http://schemas.android.com/apk/res/android"這是引用系統屬性的作用。

然而 xmlns:authcodeview="http://schemas.android.com/apk/res/com.example.authcodeview"是引用自己定義屬性。

 xmlns:+名稱 = "http://schemas.android.com/apk/res/ + 應用的包名"

後面使用時候自己定義屬性就是這樣啦

            authcodeview:titleText="3712"
            authcodeview:titleTextColor="#00ffff"
            authcodeview:titleTextSize="40sp"

順便附上系統arrs自己定義的路徑

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >


3、在自己定義控制項中class怎麼引用問題了

看一段代碼先

/** * 獲得我自己定義的樣式屬性 *  * @param context * @param attrs * @param defStyle */public AuthCodeView(Context context, AttributeSet attrs, int defStyle){super(context, attrs, defStyle);/** * 獲得我們所定義的自己定義樣式屬性 */TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AuthCodeView, defStyle, 0);//擷取在attr檔案下,名字為AuthCodeView的declare-styleable屬性有幾個int n = a.getIndexCount();for (int i = 0; i < n; i++){int attr = a.getIndex(i);switch (attr){//這個屬效能夠不要,由雩都是隨機產生case R.styleable.AuthCodeView_titleText:mTitleText = a.getString(attr);break;case R.styleable.AuthCodeView_titleTextColor:// 預設顏色設定為黑色mTitleTextColor = a.getColor(attr, Color.BLACK);break;case R.styleable.AuthCodeView_titleTextSize:// 預設設定為16sp。TypeValue也能夠把sp轉化為pxmTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));break;}}a.recycle();}


這個TypedArray的作用就是資源的映射作用。寫法是這種。

R.styleable.AuthCodeView這個是不是非常熟悉。

還有R.styleable.AuthCodeView_titleText,後面就是名稱加上下橫線加上屬性。

這樣做就把自己定義屬性在xml設定值映射到class,怎麼擷取都非常easy。 

有空格看以去看看產生id的R檔案。


這篇先到這裡結束,還有這篇的續集。自己定義屬性控制項。也是自己定義view。隨機驗證碼demo學習。


android 自己定義控制項屬性(TypedArray以及attrs解釋)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.