最近在研究android自定義控件屬性,學到了typedarray以及attrs。大家也可以結合《理解android中的自定義屬性》這篇文章進行學習,后續一篇還有應用。
1、attrs文件編寫
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?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代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<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自定義的路徑:
3、在自定義控件中class怎么引用問題了
看一段代碼先
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
/** * 獲得我自定義的樣式屬性 * * @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轉化為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設置值映射到class,怎么獲取都很簡單。
這篇先到這里結束,還有這篇的續集,自定義屬性控件,也是自定義view,隨機驗證碼demo學習詳細內容請查看《android自定義控件深入學習 android生成隨機驗證碼》。