編輯:關於 Android 程式設計
做自定義控制項時,總會希望它能在 XML 佈局裡像系統控制項一樣被設定屬性。要達成這件事,得先學會宣告自定義屬性,並在程式碼中把它讀出來。
自定義屬性集中宣告在 res/values/attr.xml,基本結構如下:
<resource> <declare-styleable name="自定義屬性名稱"> <attr name="屬性名稱" format="屬性種類"/> ...... </declare-styleable> </resource>
declare-styleable 的 name 通常取控制項的完整類名,attr 的 format 則決定這個屬性能接受哪一類值。
<declare-styleable name = "名稱">
<attr name = "background" format = "reference" />
</declare-styleable>
<ImageView android:layout_width="42dip" android:layout_height="42dip" android:background="@drawable/圖片ID" />
<declare-styleable name="名稱"> <attr name="textColor" format="color"/> </declare-styleable>
<TextView android:layout_width="42dip" android:layout_height="42dip" android:textColor="#00FF00" />
<declare-styleable name="名稱"> <attr name="focusable" format="boolean"/> </declare-styleable>
<Button android:layout_width="42dip" android:layout_height="42dip" android:focusable="true" />
<declare-styleable name="名稱"> <attr name="layout_width" format="dimension"/> </declare-styleable>
<Button android:layout_width="42dip" android:layout_height="42dip" />
<declare-styleable name="AlphaAnimation"> <attr name="fromAlpha" format="float"/> <attr name="toAlpha" format="float"/> </declare-styleable>
<alpha android:fromAlpha="1.0" android:toAlpha="0.7" />
<declare-styleable name="AnimatedRotateDrawable"> <attr name="visible"/> <attr name="frameDuration" format="integer"/> <attr name="framesCount" format="integer"/> <attr name="pivotX"/> <attr name="pivotY"/> <attr name="drawable"/> </declare-styleable>
<animated-rotate xmlns:android="https://schemas.android.com/apk/res/android" android:drawable="@drawable/圖片ID" android:pivotX="50%" android:pivotY="50%" android:framesCount="12" android:frameDuration="100" />
<declare-styleable name="MapView"> <attr name="apiKey" format="string"/> </declare-styleable>
<com.google.android.maps.MapView android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g" />
<declare-styleable name="RotateDrawable"> <attr name="visible"/> <attr name="fromDegrees" format="float"/> <attr name="toDegrees" format="float"/> <attr name="pivotX" format="fraction"/> <attr name="pivotY" format="fraction"/> <attr name="drawable"/> </declare-styleable>
<rotate xmlns:android="https://schemas.android.com/apk/res/android" android:interpolator="@anim/動畫ID" android:fromDegrees="0" android:toDegrees="360" android:pivotX="200%" android:pivotY="300%" android:duration="5000" android:repeatMode="restart" android:repeatCount="infinite" />
<declare-styleable name="名稱"> <attr name="orientation"> <enum name="horizontal" value="0"/> <enum name="vertical" value="1"/> </attr> </declare-styleable>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> </LinearLayout>
<declare-styleable name="名稱"> <attr name="windowSoftInputMode"> <flag name="stateUnspecified" value="0"/> <flag name="stateUnchanged" value="1"/> <flag name="stateHidden" value="2"/> <flag name="stateAlwaysHidden" value="3"/> <flag name="stateVisible" value="4"/> <flag name="stateAlwaysVisible" value="5"/> <flag name="adjustUnspecified" value="0x00"/> <flag name="adjustResize" value="0x10"/> <flag name="adjustPan" value="0x20"/> <flag name="adjustNothing" value="0x30"/> </attr> </declare-styleable>
<activity android:name=".StyleAndThemeActivity" android:label="@string/app_name" android:windowSoftInputMode="stateUnspecified|stateUnchanged | stateHidden"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>
宣告時用 | 串接多個 format,使用時就能自由混填:
<declare-styleable name="名稱"> <attr name="background" format="reference|color"/> </declare-styleable>
<ImageView android:layout_width="42dip" android:layout_height="42dip" android:background="@drawable/圖片ID|#00FF00" />
這兩個類別是自定義屬性在程式碼端的入口,釐清它們的角色是理解整個機制的關鍵。
AttributeSet 負責「裝」。控制項初始化的時候,系統會把佈局檔中屬於這個控制項的屬性名(key,例如 background)與對應的值(value,例如 @drawable/icon)打包成 AttributeSet,傳進 View 的建構子。對於非 Android 自帶的屬性,View 基類無法識別,必須由我們自己解析。
TypedArray 負責「說明」。AttributeSet 裡只有名稱和值,但控制項怎麼知道哪個屬性代表什麼意義、該用哪種型別去讀?這件事由 TypedArray 承擔——它封裝了 attrs.xml 中 styleable 所宣告的型別資訊,讓我們能把 AttributeSet 中的原始值正確取出並套用。
打個比方:AttributeSet 是一盒糖,TypedArray 則是盒上的標籤說明,告訴你每顆糖是什麼口味。而這盒糖究竟有哪些口味,取決於你在自己的 styleable 檔裡宣告了什麼。
理解概念之後,實際使用只需三步:在拿到 AttributeSet 的建構子裡呼叫 context.obtainStyledAttributes(attrs, R.styleable.你的屬性組) 取得 TypedArray,接著用 getColor / getDimension / getString 等方法依序讀值,最後別忘了呼叫 recycle() 釋放資源,否則會造成記憶體浪費。
Android 加固 APK 的安全分析:動態加載、DEX 保護與 WebView 風險
本文僅從授權安全測試與防禦建設的角度,討論 Android 加固 APK 的常見保護思路。加固技術本身是中性的,理解它有助於開發者設計更穩妥的防護方案,也能幫助安全人員識
Android UI 開發利器:Android Action Bar Style Generator
ActionBar 是 Android 3.0 之後的 UI 設計規範,也是 Google 極力推薦的設計風格。那麼,如何快速設計一個順眼的 ActionBar?更进一步
Android 4.3 鎖屏「Emergency calls only」文案重複問題的追與解
從圖上可以看到,這個鎖屏介面在語義上出現了重複:上方運營商區域已經顯示了「Emergency calls only」,下方緊急撥號按鈕旁又是一個類似含義的欄位。這是 G
Android 在 ListView/RecyclerView 中添加圖片選擇與上傳效果
很多 App 在做反饋、評價功能時,都需要用戶上傳多張圖片,並以九宮格形式展示、支持點擊「+」添加。本文就來實現這樣一個效果:一個能動態添加圖片、帶上傳進度、支持刪除的列