自訂汽車 UI 首選項

本頁面介紹如何使用和自訂com.android.car.ui.preference套件中的元素,其中包括可用於呈現應用程式首選項的 UI 元素。

大多數元素都是com.androidx.preference類的子類,您可以向其中添加 CarUi 自訂項目。在大多數情況下,您可以透過com.android.car.PreferenceFragment來使用com.android.car.ui.preferences套件中的元素。 PreferenceFragment將所有androidx.preference元素替換為car.ui.preferences中的等效元素。若要查看這些功能的實際應用,請參閱 car-ui-lib Paintbooth 參考應用程式。例如:

汽車 UI 偏好佈局

圖 1.汽車 UI 首選項佈局。

可用的首選項類型

本節介紹汽車 UI 首選項類型。

偏好片段

PreferenceFragmentandroidx.preference.PreferenceFragmentCompat的子類,並且是客戶端應使用的基類。在呼叫setPreferenceScreen期間,此類自動將androidx.preference中的元素替換為car.ui.preference元素中可用的等效元素。

<style name="PreferenceFragment.CarUi">
        <item name="android:divider">?android:attr/listDivider</item>
        <item name="android:layout">@layout/car_ui_preference_fragment_with_toolbar</item>
</style>

卡魯伊偏好

這是 CarUi 相當於androidx.preference.Preference類,支援以下屬性,可以使用運行時資源覆蓋 (RRO) 進行自訂。使用CarUiPreference樣式變體的所有其他 CarUi 元素也支援以下屬性。

<declare-styleable name="CarUiPreference">
      <!-- Toggle for showing chevron -->
      <attr name="showChevron" format="boolean" />
      <!-- Show ripple when disabled preference is clicked -->
      <attr name="showRippleOnDisabledPreference" format="boolean" />
</declare-styleable>

以下是com.android.car.ui.preference的基本樣式,它擴展了androidx.Preference主題。 chevron圖案在car_ui_preference_icon_chevron.xml中定義。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_enabled="false" android:drawable="@drawable/car_ui_preference_icon_chevron_disabled"/>
  <item android:state_enabled="true" android:drawable="@drawable/car_ui_preference_icon_chevron_enabled"/>
</selector>

ripplecar_ui_recyclerview_button_ripple_background中定義。

<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/car_ui_ripple_color" />
<style name="Preference.CarUi">
        <item name="allowDividerBelow">false</item>
        <item name="allowDividerAbove">false</item>
        <item name="android:layout">@layout/car_ui_preference</item>
</style>

如果上面樣式中的佈局被覆蓋,客戶端需要確保新佈局至少提供相應androidx類別所需的 id。這也適用於使用此基本樣式的其他元素。

CarUi下拉首選項

這是與androidx.preference.DropDownPreference類別等效的 CarUi,並使用以下樣式:

<style name="Preference.CarUi.DropDown">
        <item name="android:layout">@layout/car_ui_preference_dropdown</item>
</style>

CarUiRadioButton首選項

androidx.preference.TwoStatePreference的這個子類別在初始化期間會膨脹car_ui_radio_button_preference_widget佈局。如果此佈局覆蓋在 RRO 中,則新佈局必須包含android.widget.RadioButton類型的元素,其 id 為R.id.radio_button ,並且還必須包含基類所需的其他元素和 id。

CarUiSwitch首選項

這是 CarUi 中androidx.preference.SwitchPreference類別的等效項。不同之處在於,當未啟用此元素時,會顯示一條 toast 訊息。在 Android 10 和 Android 11 中,可以透過呼叫setMessageToShowWhenDisabledPreferenceClicked方法來自訂 Toast 訊息。此元素使用以下樣式:

<style name="Preference.CarUi.SwitchPreference">
        <item name="android:widgetLayout">@layout/car_ui_preference_widget_switch</item>
        <item name="android:switchTextOn">@string/car_ui_preference_switch_on</item>
        <item name="android:switchTextOff">@string/car_ui_preference_switch_off</item>
</style>

CarUiTwoActionPreference

CarUiPreference的子類,支援輔助操作。下面提供了可用於自訂此元素的屬性:

<declare-styleable name="CarUiTwoActionPreference">
        <!-- Determines if the secondary action is initially shown -->
        <attr name="actionShown" format="boolean"/>
</declare-styleable>

該元素使用與基底類別相同的樣式,並且需要R.id.action_widget_containerandroid.R.id.widget_frame

CarUiDialogFragment

此類是從androidx.preference.PreferenceDialogFragmentCompat借用的,並添加了一些特定於 CarUi 的內容。客戶端應該使用此類的子類別來呈現主題平台AlertDialog

首選項對話框片段

CarUiDialogFragment的直接子類別。客戶端應該對此類進行子類化以呈現任何主題平台AlertDialog

CarUiEditText首選項

這是androidx.preference.EditTextPreference類別的 CarUi 等效項:

<style name="Preference.CarUi.DialogPreference.EditTextPreference">
        <item name="android:dialogLayout">@layout/car_ui_preference_dialog_edittext</item>
</style>

如果上面樣式中的佈局被覆蓋,客戶端需要確保新佈局提供androidx.preference.EditTextPreference類別所需的 id。

編輯文字首選項對話框片段

PreferenceDialogFragment的直接子類別。此外,CarUi 相當於androidx.preference.PreferenceDialogFragments類別。客戶端應該使用此類來呈現任何主題平台AlertDialog

CarUiSeekBarDialog首選項

androidx.preference.DialogPreference類別的實現,提供SeekBar功能:

<style name="Preference.CarUi.SeekBarPreference">
        <item name="android:layout">@layout/car_ui_preference_widget_seekbar</item>
        <item name="adjustable">true</item>
        <item name="showSeekBarValue">false</item>
</style>

SeekBarPreferenceDialogFragment

在對話框中提供帶有 SeekBar 的佈局的片段。

CarUIList首選項

CarUi 相當於androidx.preference.ListPreference類別。

清單首選項片段

提供其中包含androidx.preference.ListPreference的佈局的片段。

CarUiMultiSelectList首選項

CarUi 相當於androidx.preference.Preference類別。

多重選取清單首選項片段

提供其中包含com.android.car.ui.CarUiMultiSelectListPreference的佈局的片段。