Od 27 marca 2025 r. zalecamy używanie android-latest-release
zamiast aosp-main
do kompilowania i wspołtworzenia AOSP. Więcej informacji znajdziesz w artykule o zmianach w AOSP.
Zmiana kolejności ustawień samochodu
Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
W większości przypadków zmiana hierarchii ustawień jest stosunkowo prosta i polega na przeniesieniu odpowiednich ustawień i elementów PreferenceController
do innego pliku XML. Jeśli w dodatku PreferenceController
używasz use(...)
, usuń go z poprzedniej SettingsFragment
i dodaj do nowej SettingsFragment
.
Na tej stronie znajdziesz przykłady zmiany kolejności ustawień w różnych sytuacjach.
Przenoszenie podstawowej preferencji
Ten przykład pokazuje, jak przenieść ustawienie z jednego ekranu ustawień na inny, na którym ma ono tylko domyślny kontroler ustawień. W tym przykładzie przenosisz ustawienie Jednostki z ekranu ustawień Strona główna na ekran ustawień System. Aby to zrobić, przenieś ten kod XML z katalogu homepage_fragment.xml
do odpowiedniego miejsca w katalogu 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>
Przenoszenie ustawienia, które używa funkcji use(...)
Rozważmy bardziej złożony przykład, w którym wszystkie ustawienia w fragmentach Ostrzeżenie o danych i Ograniczenie przesuwamy o jeden poziom w dół do fragmentu Korzystanie z danych. W ten sposób metoda DataWarningAndLimitFragment.java
zostanie zaktualizowana, aby zawierała metodę use
, która przekazuje informacje do kontrolerów ustawień po utworzeniu.
- Przenieś odpowiedni plik XML do wybranej lokalizacji w sekcji
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>
- W
DataWarningAndLimitFragment.java
określ, jak używać metody 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);
}
}
W tym przypadku metoda use
ustawia edytor zasad sieciowych i szablon sieci dla kontrolerów preferencji. Ponieważ w tym przykładzie wszystkie ustawienia i cały kod w metodie onAttach
są istotne dla ustawiania tych parametrów, warto skopiować całą zawartość metody do nowego fragmentu. Może się to jednak różnić w zależności od konkretnej preferencji. Musisz też przenieść odpowiednie zmienne instancji.
Jest jednak pewien problem. Pierwotny fragment, który powinien być przekazany jako argument NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE
, powinien pochodzić z intencji do aktywności (jeśli jest podany).
Aby uzyskać te informacje, utwórz metodę newInstance
i przekaż w niej szablon, jeśli jest obecny (w przeciwnym razie prześlij null), a następnie zaktualizuj aktywność dla DataUsageFragment
lub pobierz informacje o intencji bezpośrednio w metodie onAttach
, używając do tego metody getActivity().getIntent()
. W obu przypadkach możesz przekazać informacje wymagane w przypadku tej metody, tak jak w powyższym przykładzie.
- Zanim usuniesz stare fragmenty i pliki XML, zidentyfikuj inne zależności lub oczekiwane działania związane z intencją w starym fragmencie. W tym przypadku wartość konfiguracji nakładki wskazuje na starą aktywność, którą należy zaktualizować, aby wskazywała na właściwą aktywność.
Dodawanie ekranu ustawień do hierarchii
Aby dodać nowy ekran preferencji do hierarchii, zapoznaj się z artykułem Dodawanie ustawień samochodu.
Po utworzeniu nowego ekranu ustawień możesz na podstawie powyższych przykładów zmienić hierarchię ustawień.
Treść strony i umieszczone na niej fragmenty kodu podlegają licencjom opisanym w Licencji na treści. Java i OpenJDK są znakami towarowymi lub zastrzeżonymi znakami towarowymi należącymi do firmy Oracle lub jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2025-08-02 UTC.
[[["Łatwo zrozumieć","easyToUnderstand","thumb-up"],["Rozwiązało to mój problem","solvedMyProblem","thumb-up"],["Inne","otherUp","thumb-up"]],[["Brak potrzebnych mi informacji","missingTheInformationINeed","thumb-down"],["Zbyt skomplikowane / zbyt wiele czynności do wykonania","tooComplicatedTooManySteps","thumb-down"],["Nieaktualne treści","outOfDate","thumb-down"],["Problem z tłumaczeniem","translationIssue","thumb-down"],["Problem z przykładami/kodem","samplesCodeIssue","thumb-down"],["Inne","otherDown","thumb-down"]],["Ostatnia aktualizacja: 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."]]