ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
จัดเรียงการตั้งค่ารถใหม่
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
การจัดลําดับชั้นการตั้งค่าใหม่นั้นค่อนข้างตรงไปตรงมา และมักจะเป็นการย้ายค่ากําหนดที่เกี่ยวข้องและ PreferenceController
ไปยังไฟล์ XML อื่น หาก PreferenceController
ใช้ use(...)
โปรดนำ 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
เป็นอาร์กิวเมนต์ ซึ่งควรมาจาก Intent ไปยังกิจกรรม (หากมี)
หากต้องการรับข้อมูลที่จําเป็นนี้ ให้สร้างเมธอด newInstance
แล้วส่งเทมเพลตหากมี (หรือส่งค่า Null) จากนั้นอัปเดตกิจกรรมสําหรับ DataUsageFragment
หรือรับข้อมูล Intent โดยตรงในเมธอด onAttach
โดยใช้ getActivity().getIntent()
ไม่ว่าในกรณีใด คุณสามารถส่งข้อมูลที่จำเป็นสำหรับวิธีการนี้ได้ตามที่คุณทำด้านบน
- ระบุการพึ่งพาอื่นๆ หรือการดำเนินการตาม Intent ที่คาดไว้ในส่วนที่เก่าก่อนที่จะล้างข้อมูลส่วนที่เป็นเศษและไฟล์ 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."]]