自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
模式和组件
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在 Android 8.0 中,“设置”菜单中增加了一些能够涵盖常见使用情况的组件和微件。我们鼓励设备制造商和开发者在扩展“设置”应用时使用常用组件,以便新界面与现有的“设置”界面保持一致。
以下是改进摘要:
- 支持库偏好设置框架中的分隔线行为变化。现在,分隔线绘制在类别之间。
- ActionBar 主题背景变化。现在,ActionBar 使用浅色色彩主题,且带有强调色文本。
- 新的偏好设置布局。图标空间仍然存在,即使偏好设置没有图标时也是如此。
新的微件:
- 应用详细信息的标头 widget。显示应用图标、应用标签等信息。
- 有些页面上具有展开按钮。页面在打开时可以是收起形式,隐藏不太重要的项目,直到用户点击展开按钮。
- 默认应用选择器界面:
- 该界面用于选择默认浏览器、默认手机应用等。
- 以前是一个对话框,现在是一个基于单选按钮的全屏界面。
- “MasterSwitch”样式偏好设置。这是具有两个点击目标的偏好设置。左侧目标会导致子集化的片段或 intent。右侧目标是一个切换开关,针对整个页面控制开启/关闭。
示例和来源
- 分隔线行为
- “设置”中的所有页面均经过修改以使用新的分隔线行为。
- 分隔线行为在以下文件中定义为一个 ThemeOverlay:
packages/apps/Settings/res/values/styles_preference.xml
- ActionBar 主题变化
- “设置”中的所有页面均经过修改以使用新的 ActionBar 主题背景。
- 主题背景在 Theme.DeviceDefault.Settings 中定义
- 新的偏好设置布局
- 现在,“设置”中的许多页面都使用新的偏好设置布局。
- 您可以在以下文件中找到代码:
packages/apps/Settings/res/values/styles_preference.xml
- 应用标头 widget
- “设置”中的大多数应用信息页面都已实现新的应用标头。
- 相关示例和代码可以在以下位置找到:
packages/apps/Settings/src/com/android/settings/applications/AppHeaderController.java
- 展开按钮
- 默认应用选择器
- 您可以在以下位置找到基类的代码:
packages/apps/Settings/src/com/android/settings/applications/defaultapps/DefaultAppPickerFragment.java
- DefaultAppPickerFragment 有多个子类,每个子类均实现一个选择器用于不同的 intent。
- MasterSwitchMasterSwitch 样式偏好设置
- 代码位于:
https://cs.android.com/android/platform/superproject/main/+/main:packages/apps/Settings/src/com/android/settings/wifi/WifiPrimarySwitchPreferenceController.java
- WLAN 主开关就是一个示例用例。您可以在以下位置找到示例:
packages/apps/Settings/src/com/android/settings/wifi/WifiMasterSwitchPreferenceController.java
实现
设备制造商可以开始使用所有新的组件,无需额外设置。
如果原始设备制造商 (OEM) 决定实现新的“MasterSwitch”样式偏好设置或默认应用选择器,则应查看本文档中的示例以及为每个组件编写的参考文件 (Javadoc),以获取更多详细信息。
自定义“设置”菜单
- 分隔线行为。要更改分隔线的绘制方式,请更新“设置”分隔线的样式并更改以下各项的值:
allowDividerAbove
allowDividerBelow
allowDividerAfterLastItem
- ActionBar 主题背景颜色。Activity 应使用
Theme.DeviceDefault.Settings
作为其主题背景,或使用 Theme.DeviceDefault.Settings
作为父主题背景来创建自定义主题背景。
- 应用标头微件。在 AppHeaderController 中使用设置器自定义每个字段,并在设置所有字段后调用
build()
。
- 展开按钮:
- 要完全停用该功能,请替换 ProgressiveDisclosureMixin 的构造函数,并将 keepExpanded 设为 true。
- 要自定义最开始显示多少个项目,请在 Fragment 的
onAttach(Context)
方法期间调用 ProgressiveDisclosureMixin.setTileLimit()
方法。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],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."]]