国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2020-04-11 10:53:28
字體:
來源:轉載
供稿:網友

最近在研究android自定義控件屬性,學到了TypedArray以及attrs。大家也可以結合《理解Android中的自定義屬性》這篇文章進行學習,后續一篇還有應用。
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里面怎么引用以及使用,對比系統空間屬性
先看兩張圖,就了解大半了,也理解大半了。
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"這是引用系統屬性的作用。
然而 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怎么引用問題了
看一段代碼先

/**  * 獲得我自定義的樣式屬性  *  * @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生成隨機驗證碼》

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金秀| 莱州市| 原阳县| 吉安市| 特克斯县| 化德县| 辰溪县| 屯门区| 湘西| 类乌齐县| 屏东市| 登封市| 惠水县| 长宁区| 鲁山县| 永修县| 南雄市| 寿光市| 大名县| 牙克石市| 两当县| 合肥市| 怀宁县| 武鸣县| 抚远县| 东至县| 安塞县| 庆安县| 恩施市| 镇原县| 清徐县| 二连浩特市| 东安县| 汶上县| 金溪县| 乐昌市| 凯里市| 龙江县| 南漳县| 盐源县| 鹤庆县|