2025 年 3 月 27 日より、AOSP のビルドとコントリビューションには aosp-main
ではなく android-latest-release
を使用することをおすすめします。詳細については、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(...) を使用する設定の移動
次の複雑な例では、データ使用量の警告と制限のフラグメントのすべての設定を 1 つ上のレベルのデータ使用量フラグメントに移動させます。これにより、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
メソッドのすべてのコードが移動させる設定パラメータの設定に関連するため、メソッド コンテンツ全体を新しいフラグメントにコピーすることをおすすめします。ただし、これは個々の設定によって異なります。また、関連するインスタンス変数を移動させる必要もあります。
ただし、これは非常に複雑です。元のフラグメントは、NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE
が引数としてインテントからアクティビティ(指定されている場合)に渡されることを想定していました。
この必要な情報を取得するには、newInstance
メソッドを作成してテンプレート(存在する場合)に渡して(それ以外の場合は null を渡します)、DataUsageFragment
のアクティビティを更新するか、または getActivity().getIntent()
を使用して onAttach
メソッド内でインテントの情報を直接取得します。いずれの場合も、上記で行ったように、このメソッドに必要な情報を渡すことができます。
- 古いフラグメントや XML ファイルをクリーンアップする前に、古いフラグメントで他の依存関係または想定されるインテントのアクションを確認します。この例では、オーバーレイの構成値は古いアクティビティを指しているため、正しいアクティビティを指すように更新する必要があります。
階層への設定画面の追加
新しい設定画面を階層に追加するには、車の設定の追加をご覧ください。
新しい設定画面を作成したら、上記の例を使用して、必要に応じて構成階層を並べ替えます。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-08-02 UTC。
[[["わかりやすい","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-08-02 UTC。"],[],[],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."]]