如何更改 ListPreference 弹出对话框的样式?

How to change the style of a ListPreference popup dialog?

我正在尝试更改 ListPreference 的弹出对话框的样式,就像我在 answer 中看到的那样。例如,我想要对话框的不同背景颜色。

到目前为止,我尝试应用我的自定义样式:

<item name="android:dialogTheme">@style/AlertDialogStyle</item>
<item name="android:alertDialogTheme">@style/AlertDialogStyle</item>
<item name="android:alertDialogStyle">@style/AlertDialogStyle</item>
<item name="android:dialogPreferenceStyle">@style/AlertDialogStyle</item>


<style name="AlertDialogStyle" parent="AlertDialog.AppCompat">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textColor">@color/lightGrey</item>
    <item name="android:background">@color/cardBackground</item>
    <item name="android:popupBackground">@color/cardBackground</item>
    <item name="android:windowBackground">@color/cardBackground</item>
    <item name="android:itemBackground">@color/cardBackground</item>
</style>

但是我的风格还是不行applied/background颜色没变

这是我的 ListPreference 的弹出对话框目前的样子:

这是我要存档的颜色主题(基本上与我用于其他对话框的主题相同):


快速重现我的问题 -> 我的项目在 github

取决于你调用多少time/place,你可以创建一个 DialogFragment 来处理这个。

DialogFragment 可以这么简单:

public Dialog onCreateDialog(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mText = getArguments().getString("remark");
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        //builder.setTitle("Remarks");
        builder.setMessage(mText);
        builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        return builder.create();
    }

这将自动创建一个带有取消按钮的简单对话框。 要添加样式:,请在 super.onCreate 之后添加以下内容:

setStyle(DialogFragment.STYLE_NO_TITLE, R.style.AlertDialogStyle);

或者您可以通过创建自己的布局来做更多的自定义:fragment_dialogfragment_alert.xml 然后在 class 中,使用 onCreateView 而不是 onCreateDialog

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_dialogfragment_alert, container, false);
...
}

回答我自己的问题。最后它就像替换一样简单:

<item name="android:alertDialogTheme">@style/AlertDialogStyle</item>

<item name="alertDialogTheme">@style/AlertDialogStyle</item>

我认为您在标记中混用了各种东西。 alertDialogStyle 和 alertDialogTheme 都是不同的。

自定义警报对话框主题你应该创建你的对话框主题,一个主题应该扩展@android:style/Theme.Dialog.Alert

<item name="android:dialogTheme">@style/dialogAlertTheme</item>
<item name="android:alertDialogTheme">@style/dialogAlertTheme</item>
<item name="android:alertDialogStyle">@style/AlertDialogStyle</item>
<item name="android:dialogPreferenceStyle">@style/AlertDialogStyle</item>

<style name="dialogAlertTheme" parent="@android:style/Theme.Dialog.Alert">
    <item name="android:windowBackground">[...]</item>
    [...]
</style>

<style name="AlertDialogStyle" parent="AlertDialog.AppCompat">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textColor">@color/lightGrey</item>
    <item name="android:background">@color/cardBackground</item>
    <item name="android:popupBackground">@color/cardBackground</item>
    <item name="android:windowBackground">@color/cardBackground</item>
    <item name="android:itemBackground">@color/cardBackground</item>
</style>

注意 1。 自定义警报对话框样式仅限于提供(背景)可绘制对象。

Note-2. 自定义alert dialog theme 开启方法提供windowBackground, windowTitleStyle等属性,但需要Android版本支持主题的 alertDialogThem attribute/item

你的答案的解释: 为什么如果你从 android:alertDialogTheme 中删除 android: android:

<item name="alertDialogTheme">@style/AlertDialogStyle</item>

现在这是覆盖 AlertDialog 样式的标准方法。它是库的一部分,因为 AlertDialog 不会使用主主题中的强调色,但自 v24.2.0.0 以来已被删除,因为 Android 团队修复了此行为。

问题参考: https://github.com/Gericop/Android-Support-Preference-V7-Fix/issues/52#issuecomment-255759293

更改参考: https://github.com/Gericop/Android-Support-Preference-V7-Fix/commit/a6082cb0a508f5e0305a626c9a2a841e943ef8f6#diff-483bbb12192b1b74adadc9b4076b203b

styles.xml 中使用它并将其添加到基本主题中。此样式将警告对话框替换为 material 对话框。

    <style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
        <item name="alertDialogTheme">@style/MaterialDialogAlert</item>
        <item name="materialAlertDialogTheme">@style/MaterialDialogAlert</item>
    </style>
    <style name="MaterialDialogAlert" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
        <item name="android:textColorPrimary">?colorPrimary</item>
        <item name="android:background">?colorSurface</item>
        <item name="buttonBarPositiveButtonStyle">@style/AlertButtonStyle</item>
        <item name="buttonBarNegativeButtonStyle">@style/AlertButtonStyle</item>
        <item name="buttonBarNeutralButtonStyle">@style/AlertButtonStyle</item>
    </style>