最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2016年01月29日 11:18:06   作者:mmsx  
這篇文章主要為大家介紹了android自定義控件屬性TypedArray以及attrs,感興趣的小伙伴們可以參考一下

最近在研究android自定義控件屬性,學(xué)到了TypedArray以及attrs。大家也可以結(jié)合《理解Android中的自定義屬性》這篇文章進(jìn)行學(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> 

看到這上面的代碼有三個(gè)屬性,首先attr標(biāo)簽是定義名字以及屬性。后面是一個(gè)declare-styleable組,這個(gè)組名字AuthCodeView,后面class中會(huì)用到。

2、在xml里面怎么引用以及使用,對(duì)比系統(tǒng)空間屬性
先看兩張圖,就了解大半了,也理解大半了。
a、自定義屬性的名字的引用


b、仔細(xì)看圖上說明以及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="點(diǎn)擊驗(yàn)證碼,換一張" /> 
 </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="輸入驗(yàn)證碼" /> 
 
 <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="驗(yàn)證" /> 
 
</LinearLayout> 

重點(diǎn)看頭部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)用的包名"
后面使用時(shí)候自定義屬性就是這樣啦。

  • 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屬性有幾個(gè) 
 int n = a.getIndexCount(); 
 for (int i = 0; i < n; i++) 
 { 
 int attr = a.getIndex(i); 
 switch (attr) 
 { 
 //這個(gè)屬性可以不要,因?yàn)槎际请S機(jī)產(chǎn)生 
 case R.styleable.AuthCodeView_titleText: 
  mTitleText = a.getString(attr); 
  break; 
 case R.styleable.AuthCodeView_titleTextColor: 
  // 默認(rèn)顏色設(shè)置為黑色 
  mTitleTextColor = a.getColor(attr, Color.BLACK); 
  break; 
 case R.styleable.AuthCodeView_titleTextSize: 
  // 默認(rèn)設(shè)置為16sp,TypeValue也可以把sp轉(zhuǎn)化為px 
  mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( 
   TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics())); 
  break; 
 
 } 
 
 } 
 a.recycle(); 
 } 

這個(gè)TypedArray的作用就是資源的映射作用,寫法是這樣的。R.styleable.AuthCodeView這個(gè)是不是很熟悉。
還有R.styleable.AuthCodeView_titleText,后面就是名稱加上下橫線加上屬性。
這樣做就把自定義屬性在xml設(shè)置值映射到class,怎么獲取都很簡(jiǎn)單。

這篇先到這里結(jié)束,還有這篇的續(xù)集,自定義屬性控件,也是自定義view,隨機(jī)驗(yàn)證碼demo學(xué)習(xí)詳細(xì)內(nèi)容請(qǐng)查看《Android自定義控件深入學(xué)習(xí) Android生成隨機(jī)驗(yàn)證碼》。

相關(guān)文章

  • 詳解Android中weight的使用方法

    詳解Android中weight的使用方法

    這篇文章主要向大家介紹了詳解Android中weight的使用方法,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Android開發(fā)快速實(shí)現(xiàn)底部導(dǎo)航欄示例

    Android開發(fā)快速實(shí)現(xiàn)底部導(dǎo)航欄示例

    這篇文章主要為大家介紹了Android開發(fā)快速實(shí)現(xiàn)底部導(dǎo)航欄的示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-04-04
  • android RecyclerView添加footerview詳解

    android RecyclerView添加footerview詳解

    大家好,本篇文章主要講的是android RecyclerView添加footerview詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • Android之EditText控制禁止輸入空格和回車

    Android之EditText控制禁止輸入空格和回車

    本文主要介紹了Android中使用EditText控制禁止輸入空格和回車的實(shí)現(xiàn)代碼。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-04-04
  • 詳解Android v1、v2、v3簽名(小結(jié))

    詳解Android v1、v2、v3簽名(小結(jié))

    這篇文章主要介紹了詳解Android v1、v2、v3簽名(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Android 開發(fā)隨手筆記之使用攝像頭拍照

    Android 開發(fā)隨手筆記之使用攝像頭拍照

    在Android中,使用攝像頭拍照一般有兩種方法, 一種是調(diào)用系統(tǒng)自帶的Camera,另一種是自己寫一個(gè)攝像的界面,本篇文章給大家介紹android開發(fā)隨手筆記之使用攝像頭拍照,感興趣的朋友一起學(xué)習(xí)吧
    2015-11-11
  • 詳解Android 8.1.0 Service 中 彈出 Dialog的方法

    詳解Android 8.1.0 Service 中 彈出 Dialog的方法

    這篇文章主要介紹了Android 8.1.0 Service 中怎么彈出 Dialog問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Android Okhttp斷點(diǎn)續(xù)傳面試深入解析

    Android Okhttp斷點(diǎn)續(xù)傳面試深入解析

    這篇文章主要給大家介紹了關(guān)于Android Okhttp斷點(diǎn)續(xù)傳面試的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Android具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Android Canvas和Bitmap結(jié)合繪圖詳解流程

    Android Canvas和Bitmap結(jié)合繪圖詳解流程

    在 Android Canvas 上繪圖非常難,在繪圖時(shí)需要理解許多不同的類和概念。這篇文章中,將介紹 Android 框架中可用的一些類,它們可以讓畫布使用時(shí)更輕松
    2021-11-11
  • Qt6.5.3?Android環(huán)境配置的實(shí)現(xiàn)

    Qt6.5.3?Android環(huán)境配置的實(shí)現(xiàn)

    本文主要介紹了Qt6.5.3?Android環(huán)境配置的實(shí)現(xiàn),文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01

最新評(píng)論

辉县市| 长武县| 彭水| 内乡县| 永宁县| 乌鲁木齐市| 凤城市| 克什克腾旗| 枣庄市| 高邮市| 化德县| 甘谷县| 广西| 库车县| 南充市| 宜丰县| 吕梁市| 收藏| 望都县| 资中县| 松阳县| 堆龙德庆县| 江北区| 沛县| 壤塘县| 铁岭市| 梓潼县| 锡林郭勒盟| 淳化县| 兴安盟| 丹巴县| 睢宁县| 柳州市| 海伦市| 醴陵市| 镇雄县| 和平区| 永嘉县| 淮北市| 十堰市| 昭平县|