編輯:關於 Android 程式設計
幾乎每家公司或每個行動團隊,最終都想做出一套屬於自己的 UI 框架,把自家設計與特性融進去。這必然意味著要改動 Android 的 UI,而要做好這件事,理解系統的主題與樣式是最基礎功課。
在 frameworks/base/core/res/res/values 目錄下,有四個關鍵檔案:
themes.xml themes_device_defaults.xml styles.xml styles_device_defaults.xml
它們分別定義了系統的各種 Theme 與 Style。
主要關注 themes.xml 與 themes_device_defaults.xml。前者定義了 Android 低版本的 theme 與 Holo theme;後者定義 DeviceDefault 主題,它繼承自 Holo,本質上是「在 Holo 之上做定製」的機制,留給各家廠商發揮。
選擇邏輯寫在 Resources.java 中:
public static int selectDefaultTheme(int curTheme, int targetSdkVersion) {
return selectSystemTheme(curTheme, targetSdkVersion,
com.android.internal.R.style.Theme,
com.android.internal.R.style.Theme_Holo,
com.android.internal.R.style.Theme_DeviceDefault);
}
public static int selectSystemTheme(int curTheme, int targetSdkVersion,
int orig, int holo, int deviceDefault) {
if (curTheme != 0) {
return curTheme; // 已明確指定則尊重它
}
if (targetSdkVersion < Build.VERSION_CODES.HONEYCOMB) { // < 11
return orig;
}
if (targetSdkVersion < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {// < 14
return holo;
}
return deviceDefault;
}
結論很清晰:targetSdkVersion 小於 11 使用舊版主題;11 到 13 之間使用 Holo;14 及以上使用 DeviceDefault。這也是為什麼「把 targetSdk 調高」會讓 App 外觀突然改頭換面的原因。
系統預設的三大主題是 Theme、Theme.Holo、Theme.DeviceDefault,但在此基礎上系統還定義了大量派生主題,最典型的就是各自對應的 Light 版本(Theme.Holo.Light、Theme.DeviceDefault.Light 等),以及帶 ActionBar、NoTitleBar、Wallpaper 等變體。理解這層繼承關係後,就能依實際需求在自己的應用中挑選合適的父主題。
以 Theme 為例,一個完整的主題大致涵蓋以下幾類屬性:
<item name="colorForeground">@android:color/bright_foreground_dark</item> <item name="colorBackground">@android:color/background_dark</item> <item name="colorPressedHighlight">@color/legacy_pressed_highlight</item> <item name="colorFocusedHighlight">@color/legacy_selected_highlight</item> <item name="colorActivatedHighlight">@color/legacy_selected_highlight</item>
這一類最龐大,包含 textAppearance 系列、textColorPrimary / Secondary / Tertiary 及其 Inverse、NoDisable、DisableOnly 變體,還有 textColorHint、textColorHighlight、textColorLink、editTextColor,以及大小對應的 textAppearanceLarge / Medium / Small。
<item name="buttonStyle">@android:style/Widget.Button</item> <item name="buttonStyleSmall">@android:style/Widget.Button.Small</item> <item name="buttonStyleInset">@android:style/Widget.Button.Inset</item> <item name="buttonStyleToggle">@android:style/Widget.Button.Toggle</item> <item name="selectableItemBackground">@android:drawable/item_background</item> <item name="borderlessButtonStyle">?android:attr/buttonStyle</item>
包含 listPreferredItemHeight(預設 64dip)及其 Small / Large 變體、textAppearanceListItem,以及 listPreferredItemPaddingStart / End / Left / Right 等內距設定。
<item name="windowBackground">@android:drawable/screen_background_selector_dark</item> <item name="windowNoTitle">false</item> <item name="windowFullscreen">false</item> <item name="windowTitleSize">25dip</item> <item name="windowActionBar">false</item> <item name="windowActionModeOverlay">false</item> <item name="windowTranslucentStatus">false</item> <item name="windowSoftInputMode">stateUnspecified|adjustUnspecified</item>
dialogTheme、alertDialogTheme、alertDialogStyle,以及 dialogTitleDecorLayout、dialogCustomTitleDecorLayout 等布局指向。
panelBackground、panelFullBackground、panelColorForeground、panelTextAppearance、panelMenuListWidth 等。
scrollbarSize(預設 10dip)、scrollbarThumbHorizontal / Vertical、scrollbarFadeDuration(250)、scrollbarDefaultDelayBeforeFade(300)等。
textSelectHandleLeft / Right、textSelectHandleWindowStyle,以及 textEditPasteWindowLayout、textEditNoPasteWindowLayout、textSuggestionsWindowStyle、textCursorDrawable 等與選取手柄、貼上選單相關的資源指向。
篇幅關係,剩下的屬性留待下篇繼續列舉,並進一步深入分析具體的 style 該怎麼覆寫與繼承。
Android App 的運行環境及系統架構概覽
Android 是基於 Linux 核心、面向行動裝置的作業系統。整體架構可以從上到下理解為應用層、應用框架、系統服務與執行環境、硬體抽象層,以及 Linux 核心。
Android 多進程入門:開啟方式、設定重點與常見副作用
為什麼要開啟多進程在 Android 應用開發中,多進程並不是目的,而是為了因應特定需求的一種資源規劃方式。早期系統為單一進程分配的可用記憶體有限,即使後續硬體與系統版本
React Native ActivityIndicator 加載指示器使用說明
在 React Native 中,ActivityIndicator 是常見的加載指示器元件,常被用來表示資料讀取、網路請求或畫面初始化中的等待狀態。它的外觀類似轉動的菊
使用開源庫 MagicalRecord:讓 Core Data 操作更簡潔
MagicalRecord 開源地址:https://github.com/magicalpanda/MagicalRecord 需要留意的是,MagicalRecord