欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年01月29日 11:18:06   作者:mmsx  
這篇文章主要為大家介紹了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ù)的方法詳解

    這篇文章主要介紹了Android編程獲取GPS數(shù)據(jù)的方法,結(jié)合實例形式分析了Android地理位置操作的相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下
    2016-10-10
  • Android自定義控件案例匯總2(自定義開關(guān)、下拉刷新、側(cè)滑菜單)

    Android自定義控件案例匯總2(自定義開關(guān)、下拉刷新、側(cè)滑菜單)

    這篇文章主要介紹了Android自定義控件案例匯總,自定義開關(guān)、Listview實現(xiàn)下拉刷新、側(cè)滑菜單,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android 中Banner的使用詳解

    Android 中Banner的使用詳解

    這篇文章主要介紹了Android 中Banner的使用詳解,需要的朋友可以參考下
    2017-06-06
  • Android中常用的XML生成方法實例分析

    Android中常用的XML生成方法實例分析

    這篇文章主要介紹了Android中常用的XML生成方法,以實例形式較為詳細的分析了Android生成XML的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • Android 如何查看Wifi密碼

    Android 如何查看Wifi密碼

    這篇文章主要介紹了Android 如何查看Wifi密碼的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • Android布局之TableLayout表格布局

    Android布局之TableLayout表格布局

    Tablelayout類以行和列的形式對控件進行管理,每一行為一個TableRow對象,或一個View控件。當(dāng)為TableRow對象時,可在TableRow下添加子控件,默認情況下,每個子控件占據(jù)一列。 當(dāng)為View時,該View將獨占一行
    2015-12-12
  • Jetpack Compose按鈕組件使用實例詳細講解

    Jetpack Compose按鈕組件使用實例詳細講解

    這篇文章主要介紹了Jetpack Compose按鈕組件使用實例,按鈕組件Button是用戶和系統(tǒng)交互的重要組件之一,它按照Material Design風(fēng)格實現(xiàn),我們先看下Button的參數(shù)列表,通過參數(shù)列表了解下Button的整體功能
    2023-04-04
  • Android實現(xiàn)可輸入數(shù)據(jù)的彈出框

    Android實現(xiàn)可輸入數(shù)據(jù)的彈出框

    這篇文章主要為大家詳細介紹了Android實現(xiàn)可輸入數(shù)據(jù)的彈出框,文章提供了兩種方式,示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Android繪圖之Paint的使用方法詳解

    Android繪圖之Paint的使用方法詳解

    這篇文章主要給大家介紹了關(guān)于Android繪圖之Paint使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,并給大家介紹了DrawText 基線確定的方法,需要的朋友可以參考借鑒,下面隨著小編來一些學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • Android Studio IDE升級4.1以后Start Failed

    Android Studio IDE升級4.1以后Start Failed

    這篇文章主要介紹了Android Studio IDE升級4.1以后Start Failed,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10

最新評論