一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - 詳解Android自定義控件屬性TypedArray以及attrs

詳解Android自定義控件屬性TypedArray以及attrs

2021-05-21 13:23mmsx Android

這篇文章主要為大家介紹了android自定義控件屬性TypedArray以及attrs,感興趣的小伙伴們可以參考一下

最近在研究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、自定義屬性的名字的引用

詳解Android自定義控件屬性TypedArray以及attrs

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

詳解Android自定義控件屬性TypedArray以及attrs

怕上面圖片看不清,附上部分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自定義的路徑:

詳解Android自定義控件屬性TypedArray以及attrs

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生成隨機驗證碼》

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美女沟厕撒尿全过程高清图片 | 小寡妇好紧进去了好大看视频 | 日韩一区二区三区免费 | ysl千人千色t9t9t9t9 | 调教催眠改造np总攻 | 香蕉久久一区二区不卡无毒影院 | 欧美三级不卡视频 | 亚洲欧美精品一区天堂久久 | 四虎1515hh.com | 欧美xbxbxbxb大片 | 四虎在线网站 | tobu8中国在线观看免费视频 | 国产成人啪精品午夜在线观看 | 国产成人一区二区三区视频免费蜜 | 精品久久久久久久国产潘金莲 | 美国艳星lisann成人作品 | 国产乱子伦真实china | 亚洲第一福利视频 | 国内精品久久久久久不卡影院 | 日产乱码卡1卡2卡三卡四在线 | 欧美视频一区二区专区 | nxgx在线观看国产中文 | 免费超级乱淫播放手机版 | 亚洲国产精品二区久久 | 青草青草久热精品视频在线网站 | 成年人黄视频在线观看 | 女娃开嫩苞经历小说 | 久久99r66热这里有精品 | 亚洲成A人片在线观看中文L | 男女姓交大视频免费观看 | 精品精品国产自在香蕉网 | 美女被狂揉下部羞羞动漫 | 嗯好爽视频 | 国产欧美久久久精品影院 | 亚洲国产精品综合久久网络 | 国产女王女m视频vk 国产农村一级特黄α真人毛片 | 麻豆找网服 | 国产激情在线 | 日本韩国推理片免费观看网站 | 国产精品青青青高清在线密亚 | 午夜国产理论 |