編輯:關於 Android 程式設計
近來在研究 Android 自定義控件屬性,重點接觸了 TypedArray 與 attrs。本文將系統性地梳理它們的用法,後續還會有實際應用篇章。
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 中如何引用與使用
先看兩張示意圖,就能理解大半:


對比這兩張圖,你就能明白屬性名改變後該如何引用。以下附上部分 XML 程式碼:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" xmlns:authcodeview="https://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>
重點在於頭部:xmlns:android="..." 是引用系統屬性的命名空間,而 xmlns:authcodeview="https://schemas.android.com/apk/res/com.example.authcodeview" 則是引用自定義屬性。命名規則為:xmlns:+名稱 = "https://schemas.android.com/apk/res/ + 應用的包名"。使用時寫成 authcodeview:titleText、authcodeview:titleTextColor、authcodeview:titleTextSize 即可。
3、在自定義控件的 class 中如何引用
關鍵在於透過 TypedArray 取得屬性:
/** * 獲得我自定義的樣式屬性 * * @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 裡,取值時格外方便。若需要繼續深入,可參閱續篇《Android 自定義控件深入學習 Android 生成隨機驗證碼》。
Android XML 資料的三種解析方式:DOM、SAX 與 Pull 對比
Android 平台上解析 XML 主要有三種選擇:DOM、SAX 與 Pull。本文會把三者都實作一遍,最後說明 SAX 與 Pull 在本質上的差異。 內容提要 X
扣丁音樂(三):UI 框架的實作
一、PagerSlidingTabStrip 的運用 扣丁音樂 1.0 前半段(受限於 gif 大小)演示: 影片教學中直接沿用 PagerSlidingTabStri
Android 增量更新實戰:用 bsdiff 生成補丁、bspatch 端上合成
增量更新到底是什麼 增量更新其實並不是新鮮事物,早在 Android 早期就已經有團隊在用了,只是真正把它做成產品能力的,大多是有實力的大廠。做法並不神秘:服務端保存舊版
Android JNI 入門:用 C 語言實作字串加密與解密
為什麼使用 JNI 做加密 Java 層的加密邏輯容易被反編譯分析,若希望提高逆向成本,可把部分關鍵處理放到 C 語言中,再透過 JNI 與 Java 互動。這樣的架構適