自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
重新排列汽车设置
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在大多数情况下,重新排列“设置”层次结构的过程相对简单,通常涉及将相关偏好设置和 PreferenceController
移到其他 XML 文件中。如果 PreferenceController
使用 use(...)
,请务必将其从以前的 SettingsFragment
中移除,并将其添加到新的 SettingsFragment
中。
本页面提供了对“设置”进行重新排序的示例,可以查看可能发生的情况。
移动基本偏好设置
此示例介绍了如何将偏好设置从一个偏好设置屏幕移动到另一个偏好设置屏幕,其中偏好设置只有一个默认偏好设置控制器。在此示例中,您将单位偏好设置从首页偏好设置屏幕移到系统偏好设置屏幕中。为此,将以下 XML 从 homepage_fragment.xml
移到 system_settings_fragment.xml
中的相应位置:
<Preference
android:icon="@drawable/ic_settings_units"
android:key="@string/pk_units_settings_entry"
android:title="@string/units_settings"
settings:controller="com.android.car.settings.common.DefaultRestrictionsPreferenceController">
<intent android:targetPackage="com.android.car.settings"
android:targetClass="com.android.car.settings.common.CarSettingActivities$UnitsSettingsActivity"/>
</Preference>
移动使用 use(...) 的偏好设置
注意下面这个较为复杂的示例,在该示例中,“数据流量警告和上限”fragment 中的所有偏好设置都移动到上一级的“流量消耗”fragment 中,后者更新 DataWarningAndLimitFragment.java
以包含 use
方法,从而在构建后将信息传递到偏好设置控制器。
- 将相关的 XML 移到
data_usage_fragment.xml
中的相应位置:
<Preference
android:key="@string/pk_data_usage_cycle"
android:title="@string/app_usage_cycle"
settings:controller="com.android.car.settings.datausage.CycleResetDayOfMonthPickerPreferenceController"/>
<com.android.car.settings.common.LogicalPreferenceGroup
android:key="@string/pk_data_warning_group"
settings:controller="com.android.car.settings.datausage.DataWarningPreferenceController">
<SwitchPreference
android:key="@string/pk_data_set_warning"
android:title="@string/set_data_warning"/>
<Preference
android:key="@string/pk_data_warning"
android:title="@string/data_warning"/>
</com.android.car.settings.common.LogicalPreferenceGroup>
<com.android.car.settings.common.LogicalPreferenceGroup
android:key="@string/pk_data_limit_group"
settings:controller="com.android.car.settings.datausage.DataLimitPreferenceController">
<SwitchPreference
android:key="@string/pk_data_set_limit"
android:title="@string/set_data_limit"/>
<Preference
android:key="@string/pk_data_limit"
android:title="@string/data_limit"/>
</com.android.car.settings.common.LogicalPreferenceGroup>
- 在
DataWarningAndLimitFragment.java
中,确定如何使用 use
方法。
@Override
public void onAttach(Context context) {
super.onAttach(context);
mPolicyEditor = new NetworkPolicyEditor(NetworkPolicyManager.from(context));
mNetworkTemplate = getArguments().getParcelable(
NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE);
if (mNetworkTemplate == null) {
mTelephonyManager = context.getSystemService(TelephonyManager.class);
mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
mNetworkTemplate = DataUsageUtils.getMobileNetworkTemplate(mTelephonyManager,
DataUsageUtils.getDefaultSubscriptionId(mSubscriptionManager));
}
// Loads the current policies to the policy editor cache.
mPolicyEditor.read();
List<DataWarningAndLimitBasePreferenceController> preferenceControllers =
Arrays.asList(
use(CycleResetDayOfMonthPickerPreferenceController.class,
R.string.pk_data_usage_cycle),
use(DataWarningPreferenceController.class, R.string.pk_data_warning_group),
use(DataLimitPreferenceController.class, R.string.pk_data_limit_group));
for (DataWarningAndLimitBasePreferenceController preferenceController :
preferenceControllers) {
preferenceController.setNetworkPolicyEditor(mPolicyEditor);
preferenceController.setNetworkTemplate(mNetworkTemplate);
}
}
在此示例中,use
方法用于为偏好设置控制器设置网络政策编辑器和网络模板。由于此示例移动了所有偏好设置,并且 onAttach
方法中的所有代码都与设置这些偏好设置参数相关,因此将整个方法内容复制到新的 fragment 中比较合适。但是,这具体取决于特定偏好设置。此外,您还需要移动相关实例变量。
不过,还存在一个复杂问题。原始 fragment 应以参数的形式传入 NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE
,该参数应从 intent 传递到 activity(如果提供)。
如需获取该所需信息,请创建 newInstance
方法,并传入模板(如存在,否则传入 null),然后更新 DataUsageFragment
的 activity,或使用 getActivity().getIntent()
直接在 onAttach
方法中获取 intent 信息。无论是哪种情况,您都可以按上述方式传入此方法所需的信息。
- 在清理旧 fragment 和 XML 文件之前,请确定旧 fragment 中的任何其他依赖项或预期 intent 操作。在这种情况下,叠加层配置值指向旧 activity,更新后才能指向正确的 activity。
向层次结构添加偏好设置屏幕
如需向层次结构添加新的偏好设置屏幕,请参阅添加汽车设置。
创建新的偏好设置屏幕后,您可以按照上面的示例,根据需要调整偏好设置层次结构。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-02。
[null,null,["最后更新时间 (UTC):2025-08-02。"],[],[],null,["# Rearrange Car Settings\n\nFor the most part, rearranging the Settings hierarchy is relatively\nstraightforward and typically consists of moving the relevant preference and\n`PreferenceController` to a different XML file. If the\n`PreferenceController` uses `use(...)`, be sure to remove\nit from the previous `SettingsFragment` and add it to the new\n`SettingsFragment`.\n\nThis page provides examples for reordering Settings to review situations that\nmay occur.\n\nMove a basic preference\n-----------------------\n\nThis example explains how to move a preference from one preference screen to another,\nin which the preference has just a default preference controller. In this example, you\nmove the Units preference from the Homepage preference screen into the System preference\nscreen. To do so, move following XML from `homepage_fragment.xml` into the\nappropriate location in `system_settings_fragment.xml`: \n\n```transact-sql\n\u003cPreference\n android:icon=\"@drawable/ic_settings_units\"\n android:key=\"@string/pk_units_settings_entry\"\n android:title=\"@string/units_settings\"\n settings:controller=\"com.android.car.settings.common.DefaultRestrictionsPreferenceController\"\u003e\n \u003cintent android:targetPackage=\"com.android.car.settings\"\n android:targetClass=\"com.android.car.settings.common.CarSettingActivities$UnitsSettingsActivity\"/\u003e\n \u003c/Preference\u003e\n```\n\nMove a preference that uses use(...)\n------------------------------------\n\nConsider the following more complex example that moves all the preferences\nin the Data Warning \\& Limit fragment up one level into the Data Usage fragment, which\nupdates `DataWarningAndLimitFragment.java` to include the `use` method\nto pass information into the preference controllers after construction.\n\n1. Move the relevant XML to the desired location in `data_usage_fragment.xml`: \n\n ```transact-sql\n \u003cPreference\n android:key=\"@string/pk_data_usage_cycle\"\n android:title=\"@string/app_usage_cycle\"\n settings:controller=\"com.android.car.settings.datausage.CycleResetDayOfMonthPickerPreferenceController\"/\u003e\n \u003ccom.android.car.settings.common.LogicalPreferenceGroup\n android:key=\"@string/pk_data_warning_group\"\n settings:controller=\"com.android.car.settings.datausage.DataWarningPreferenceController\"\u003e\n \u003cSwitchPreference\n android:key=\"@string/pk_data_set_warning\"\n android:title=\"@string/set_data_warning\"/\u003e\n \u003cPreference\n android:key=\"@string/pk_data_warning\"\n android:title=\"@string/data_warning\"/\u003e\n \u003c/com.android.car.settings.common.LogicalPreferenceGroup\u003e\n \u003ccom.android.car.settings.common.LogicalPreferenceGroup\n android:key=\"@string/pk_data_limit_group\"\n settings:controller=\"com.android.car.settings.datausage.DataLimitPreferenceController\"\u003e\n \u003cSwitchPreference\n android:key=\"@string/pk_data_set_limit\"\n android:title=\"@string/set_data_limit\"/\u003e\n \u003cPreference\n android:key=\"@string/pk_data_limit\"\n android:title=\"@string/data_limit\"/\u003e\n \u003c/com.android.car.settings.common.LogicalPreferenceGroup\u003e\n ```\n2. In `DataWarningAndLimitFragment.java`, determine how the `use` method is used. \n\n ```transact-sql\n @Override\n public void onAttach(Context context) {\n super.onAttach(context);\n\n mPolicyEditor = new NetworkPolicyEditor(NetworkPolicyManager.from(context));\n mNetworkTemplate = getArguments().getParcelable(\n NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE);\n if (mNetworkTemplate == null) {\n mTelephonyManager = context.getSystemService(TelephonyManager.class);\n mSubscriptionManager = context.getSystemService(SubscriptionManager.class);\n mNetworkTemplate = DataUsageUtils.getMobileNetworkTemplate(mTelephonyManager,\n DataUsageUtils.getDefaultSubscriptionId(mSubscriptionManager));\n }\n\n // Loads the current policies to the policy editor cache.\n mPolicyEditor.read();\n\n List\u003cDataWarningAndLimitBasePreferenceController\u003e preferenceControllers =\n Arrays.asList(\n use(CycleResetDayOfMonthPickerPreferenceController.class,\n R.string.pk_data_usage_cycle),\n use(DataWarningPreferenceController.class, R.string.pk_data_warning_group),\n use(DataLimitPreferenceController.class, R.string.pk_data_limit_group));\n\n for (DataWarningAndLimitBasePreferenceController preferenceController :\n preferenceControllers) {\n preferenceController.setNetworkPolicyEditor(mPolicyEditor);\n preferenceController.setNetworkTemplate(mNetworkTemplate);\n }\n }\n ```\n\n In this case, the `use` method sets the network policy editor\n and network template for the preference controllers. Because this example moves all\n the preferences and all the code in the `onAttach` method is relevant to\n setting these preference parameters, it would be appropriate to copy over the entire\n method contents into the new fragment. However, this varies depending on the\n specific preference. You need to also move over the relevant instance variables.\n\n However, there is a complication. The original fragment expected\n `NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE` to be\n passed in as an argument, which should come in from the intent to the activity\n (when provided).\n\n To get this needed information, either create a `newInstance`\n method and pass in the template when present (otherwise pass in null) and then\n update the activity for the `DataUsageFragment` or get the intent\n information directly in the `onAttach` method by using\n `getActivity().getIntent()`. In either case, you can pass in the\n needed information for this method as you did above.\n3. Identify any other dependencies or expected intent actions in the old fragment before cleaning up the old fragments and XML files. In this case, an [overlay\n config value](https://android.googlesource.com/platform/packages/services/Car/+/refs/heads/android11-release/car_product/overlay/frameworks/base/core/res/res/values/config.xml#98) points to the old activity, which must be updated to point to the correct activity.\n\nAdd a preference screen to the hierarchy\n----------------------------------------\n\nTo add a new preference screen to the hierarchy, see [Add Car Settings](/docs/automotive/hmi/car_settings/add_car_settings).\n\nAfter creating the new preference screen, use the examples above to rearrange the\npreference hierarchy as desired."]]