編輯:進階開發
自訂 View 大家都不陌生,但「怎麼讓自訂 View 在 XML 裡吃屬性」這件事,很多人卡在命名空間上。這篇把它一次講清楚。
如果是整個遊戲畫面或全窗介面,直接在 onCreate 裡 setContentView(myView) 就好。若當成一個可重複使用的控制項,就該在版面 XML 中宣告:
<cn.com.android123.CwjView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
也可以透過父類別的 class 屬性指定:
<View class="cn.com.android123.CwjView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
上面用到的 layout_width、layout_height 來自 android 命名空間。但自訂 View 若要做的事更多,就需要自己的屬性:
<cn.com.android123.CwjView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
cwj:age="22"
cwj:university="sjtu"
cwj:city="shanghai" />
標準 XML 需要 xmlns 宣告。系統自帶的是:
xmlns:android="https://schemas.android.com/apk/res/android"
而我們自訂的 cwj 命名空間,兩種寫法都可行:
xmlns:cwj="https://schemas.android.com/apk/res/cn.com.android123.cwjView" xmlns:cwj="https://schemas.android.com/apk/res/android"
差別在於:前者綁定到自訂 View 的完整類別名稱,後者沿用 android 命名空間的 URI。兩者 Android 都能解析,但建議使用前者,語意較清楚。
在 res/values/ 下新增一個檔案(例如 cwj_attr.xml,記得用 UTF-8 編碼):
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<declare-styleable name="CwjView">
<attr name="age" format="integer" />
<attr name="city" format="string" />
<attr name="university" format="string" />
</declare-styleable>
</resources>
declare-styleable 的 name 對應自訂 View 的類別名稱,這個名字會決定 R 檔裡產生的常數前綴。
integer:整數,例如 ProgressBar 的進度值float:浮點,例如 RatingBar 的 3.5 顆星boolean:布林,例如 ToggleButton 的勾選狀態string:字串,例如 TextView 的 textcolor:顏色,可辨識 #FF0000 這類寫法dimension:尺寸,如 23dip、15px、18spreference:資源引用,如 @+id/cwj、@drawable/xxx多種類型可以用位元或運算子合併,讓同一個屬性既能寫實際值也能寫資源引用:
<attr name="red" format="color|reference" />
最正規的方式是 obtainStyledAttributes:
public CwjView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CwjView);
mAge = a.getInteger(R.styleable.CwjView_age, 22);
mCity = a.getString(R.styleable.CwjView_city, "shanghai");
mUniversity = a.getString(R.styleable.CwjView_university, "sjtu");
a.recycle(); // 別忘了回收,TypedArray 持有原生資源
}
getXxx() 的最後一個參數是預設值——XML 裡沒寫就採用它。這個重載版本(帶 AttributeSet 的建構子)只有在透過 XML 膨脹時才會被呼叫,用 new 直接建立的情況不會走到,設計 API 時要留意。
若你想直接拿到資源 ID(例如 @drawable/xxx 的整數值),可以用:
public CwjView(Context context, AttributeSet attrs) {
super(context, attrs);
int resId = attrs.getAttributeResourceValue(
"cn.com.android123.CwjView", "age", 100);
String city = attrs.getAttributeValue(
"cn.com.android123.CwjView", "city");
}
最後一個參數同樣是找不到時的預設值。這種寫法更直接,但少了型別轉換的保護,我個人在多數情況下還是偏好 obtainStyledAttributes——它會幫你處理 color、dimension 這類需要換算的型別,也比較容易擴充。
Android 開源平台全解析:從 OHA 到 Dalvik 的技術骨架
Android 這個名字本意是「機器人」,正式身分是 Google 於 2007 年 11 月 5 日公布的、以 Linux 為基礎的開源智慧手機作業系統。 平台由作業系
實戰教學:用 PhoneGap Build 把 Sencha Touch 2 專案線上打包成 Android APK
動手之前,先準備這些東西 想把用 Sencha Touch 2 寫好的行動版網站變成可以安裝的 APK,其實不必在本地架設整套 Android 開發環境。整個流程可以拆成
從按鍵到版本:Android 開源自由背後的分裂代價
Android 到底有多少款手機?這問題恐怕很少有人答得上來。就算勉強說出個大概,要把安裝各版本系統的機型數量講清楚,更是難上加難——更別說像聯想「樂Phone」、創新工
Android聊天客戶端Demo開發:登入、列表與界面優化思路
從Demo開始打造聊天應用 聊天客戶端是學習Android開發的好題目,因為它涵蓋登入、資料傳輸、列表顯示與訊息發佈等常見功能。即使是Demo版本,也能幫助開發者理解網路