詳解Android自定義控件屬性TypedArray以及attrs
最近在研究android自定義控件屬性,學(xué)到了TypedArray以及attrs。大家也可以結(jié)合《理解Android中的自定義屬性》這篇文章進行學(xué)習(xí),后續(xù)一篇還有應(yīng)用。
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里面怎么引用以及使用,對比系統(tǒng)空間屬性
先看兩張圖,就了解大半了,也理解大半了。
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"這是引用系統(tǒng)屬性的作用。
然而 xmlns:authcodeview="http://schemas.android.com/apk/res/com.example.authcodeview"是引用自定義屬性。
xmlns:+名稱 = "http://schemas.android.com/apk/res/ + 應(yīng)用的包名"
后面使用時候自定義屬性就是這樣啦。
- authcodeview:titleText="3712"
- authcodeview:titleTextColor="#00ffff"
- authcodeview:titleTextSize="40sp"
順便附上系統(tǒng)arrs自定義的路徑:
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) { //這個屬性可以不要,因為都是隨機產(chǎn)生 case R.styleable.AuthCodeView_titleText: mTitleText = a.getString(attr); break; case R.styleable.AuthCodeView_titleTextColor: // 默認顏色設(shè)置為黑色 mTitleTextColor = a.getColor(attr, Color.BLACK); break; case R.styleable.AuthCodeView_titleTextSize: // 默認設(shè)置為16sp,TypeValue也可以把sp轉(zhuǎn)化為px mTitleTextSize = 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設(shè)置值映射到class,怎么獲取都很簡單。
這篇先到這里結(jié)束,還有這篇的續(xù)集,自定義屬性控件,也是自定義view,隨機驗證碼demo學(xué)習(xí)詳細內(nèi)容請查看《Android自定義控件深入學(xué)習(xí) Android生成隨機驗證碼》。
相關(guān)文章
Android編程獲取GPS數(shù)據(jù)的方法詳解
這篇文章主要介紹了Android編程獲取GPS數(shù)據(jù)的方法,結(jié)合實例形式分析了Android地理位置操作的相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下2016-10-10Android自定義控件案例匯總2(自定義開關(guān)、下拉刷新、側(cè)滑菜單)
這篇文章主要介紹了Android自定義控件案例匯總,自定義開關(guān)、Listview實現(xiàn)下拉刷新、側(cè)滑菜單,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12Android實現(xiàn)可輸入數(shù)據(jù)的彈出框
這篇文章主要為大家詳細介紹了Android實現(xiàn)可輸入數(shù)據(jù)的彈出框,文章提供了兩種方式,示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01Android Studio IDE升級4.1以后Start Failed
這篇文章主要介紹了Android Studio IDE升級4.1以后Start Failed,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10