自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
模式和元件
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在 Android 8.0 中,「設定」選單新增了幾個涵蓋常見用途的元件和小工具。我們建議裝置製造商和開發人員在擴充「設定」應用程式時使用常見元件,以便新的使用者介面與現有的「設定」UI 保持一致。
以下是改善項目摘要:
- 支援資料庫偏好設定架構中的分隔符行為變更。分隔線現在會在類別之間繪製。
- ActionBar 主題變更。ActionBar 現在使用淺色主題,並搭配輔色文字。
- 新的偏好設定版面配置。即使偏好設定沒有圖示,圖示空間仍會保留。
新增小工具:
- 應用程式詳細資料的標頭小工具。顯示應用程式圖示、應用程式標籤和其他資訊。
- 部分網頁上的展開按鈕。網頁一開始可以處於收合狀態,並隱藏較不重要的項目,直到使用者點選展開按鈕為止。
- 預設應用程式挑選器 UI:
- 選擇預設瀏覽器、預設電話應用程式等的使用者介面。
- 原本是對話方塊,現在則是全螢幕的圓形按鈕式 UI。
- 「MasterSwitch」樣式偏好設定。這是具有兩個點擊目標的偏好設定。左側目標會導向子集片段或意圖。右側目標是切換鈕,可控制整個網頁的開啟/關閉狀態。
範例和來源
- 分隔線行為
- 系統會修改「設定」中的所有頁面,以便使用新的分隔符行為。
- 分隔線行為定義為 ThemeOverlay:
packages/apps/Settings/res/values/styles_preference.xml
- ActionBar 主題變更
- 系統會修改「設定」中的所有頁面,以便使用新的 ActionBar 主題。
- 主題是在 Theme.DeviceDefault.Settings 中定義
- 新的偏好設定版面配置
- 設定中的許多頁面現在都採用新的偏好設定版面配置。
- 你可以在以下位置找到這組代碼:
packages/apps/Settings/res/values/styles_preference.xml
- 應用程式標題欄小工具
- 設定中的大多數應用程式資訊頁面都已導入新的應用程式標頭。
- 您可以在以下網址找到範例和程式碼:
packages/apps/Settings/src/com/android/settings/applications/AppHeaderController.java
- 展開按鈕
- 預設應用程式挑選器
- 您可以在以下位置找到基礎類別的程式碼:
packages/apps/Settings/src/com/android/settings/applications/defaultapps/DefaultAppPickerFragment.java
- DefaultAppPickerFragment 有幾個子類別,每個子類別都會為不同的意圖實作選擇器。
- MasterSwitch 樣式偏好設定
- 程式碼位於:
https://cs.android.com/android/platform/superproject/+/main:packages/apps/Settings/src/com/android/settings/wifi/WifiPrimarySwitchPreferenceController.java
- 用途範例為 Wi-Fi 主要切換器。您可以在以下位置找到範例:
packages/apps/Settings/src/com/android/settings/wifi/WifiMasterSwitchPreferenceController.java
實作
裝置製造商可以開始使用所有全新元件。如果原始設備製造商 (OEM) 決定實作新的「MasterSwitch」樣式偏好設定或預設應用程式挑選器,則應參考本文中的範例,以及使用各個元件編寫的參考檔案 (Javadoc),進一步瞭解詳情。
自訂設定選單
- 分隔符行為。如要變更分隔線的繪製方式,請更新「設定」分隔線的樣式,並變更下列項目的值:
allowDividerAbove
allowDividerBelow
allowDividerAfterLastItem
- ActionBar 主題顏色。活動應使用
Theme.DeviceDefault.Settings
做為主題,或使用 Theme.DeviceDefault.Settings
做為父項建立自訂主題。
- 應用程式頁首小工具。在 AppHeaderController 中使用 setter 自訂每個欄位,並在所有欄位設定完成後呼叫
build()
。
- 展開按鈕:
- 如要完全停用這項功能,請覆寫 ProgressiveDisclosureMixin 的建構函式,並將 keepExpanded 設為 true。
- 如要自訂要顯示的項目數量,請在片段的
onAttach(Context)
方法中呼叫 ProgressiveDisclosureMixin.setTileLimit()
方法。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Patterns and components\n\nIn Android 8.0, the Settings menu gains several components and widgets that\ncover common uses. Device manufacturers and developers are encouraged to use the\ncommon components when extending the Settings app so new user interfaces stay\nconsistent with the existing Settings UI.\n\n\nHere is a summary of improvements:\n\n- Divider behavior change in support library Preference framework. Divider is now drawn between categories.\n- ActionBar theme change. The ActionBar now uses light color theme, with accent color text.\n- New preference layout. The space for icons remains even when a preference has no icon.\n\n\nNew widgets:\n\n- A header widget for app details. Displays app icon, app label and other information.\n- An expand button on some pages. Page can start as collapsed and hide less important items until user clicks expand button.\n- Default app picker UI:\n - The UI for choosing default browser, default phone app, etc.\n - Formerly a dialog, now it's a full screen radio button-based UI.\n- A \"MasterSwitch\" style preference. This is a preference with two click targets. Left target leads to a subsetting fragment or intent. Right target is a switch toggle, controlling on/off for the entire page.\n\nExamples and source\n-------------------\n\n- Divider behavior\n - All pages in Settings are modified to use the new divider behavior.\n - The divider behavior is defined as a ThemeOverlay in: \n `packages/apps/Settings/res/values/styles_preference.xml`\n- ActionBar theme change\n - All pages in Settings are modified to use the new ActionBar theme.\n - The theme is defined in Theme.DeviceDefault.Settings\n- New preference layout\n - Many pages in Settings are now using the new preference layout.\n - You can find the code in: \n `packages/apps/Settings/res/values/styles_preference.xml`\n- App header widget\n - Most app information pages in Settings are already implementing the new App header.\n - Examples and code can be found at: \n `packages/apps/Settings/src/com/android/settings/applications/AppHeaderController.java`\n- Expand button\n - Examples and code can be found at: \n `packages/apps/Settings/src/com/android/settings/dashboard/ProgressiveDisclosureMixin.java`\n\n **Note:** This component must be used together with\n DashboardFragment. (See more details about DashboardFragment in [Updated Information Architecture](/docs/core/settings/info-architecture).)\n- Default app picker\n - You can find the code for base class in: \n `packages/apps/Settings/src/com/android/settings/applications/defaultapps/DefaultAppPickerFragment.java`\n - There are several subclasses of DefaultAppPickerFragment, each implementing a picker for different intent.\n- *MasterSwitch* style preference\n - Code is at: `https://cs.android.com/android/platform/superproject/+/main:packages/apps/Settings/src/com/android/settings/wifi/WifiPrimarySwitchPreferenceController.java`\n - An example use case is Wi-Fi primary switch. You can find an example at: `packages/apps/Settings/src/com/android/settings/wifi/WifiMasterSwitchPreferenceController.java`\n\nImplementation\n--------------\n\n\nDevice manufacturers can start using all of the new components out of the box.\nIf OEMs decide to implement a new \"MasterSwitch\" style preference or default app\npicker, they should follow the examples in this document and the reference files\n(Javadoc) written with each component for more details.\n\nCustomize the settings menu\n---------------------------\n\n- Divider behavior. To change how divider is drawn, update the style for Settings dividers and change the value for the following:\n - `allowDividerAbove`\n - `allowDividerBelow`\n - `allowDividerAfterLastItem`\n- ActionBar theme color. Activities should use `Theme.DeviceDefault.Settings` as their theme, or create a custom theme using `Theme.DeviceDefault.Settings` as parent.\n- App header widget. Use setters in AppHeaderController to customize each field and call `build()` once all fields are set.\n- Expand button:\n - To fully disable the functionality, override the constructor for ProgressiveDisclosureMixin and set keepExpanded to true.\n - To customize how many items to show initially, call the `ProgressiveDisclosureMixin.setTileLimit()` method during fragment's `onAttach(Context)` method."]]