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

20

我正在尝试改变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>

但我的风格仍未应用,背景颜色未更改。
这是目前我的ListPreference弹出对话框的样子:

enter image description here

这就是我想要实现的颜色主题(基本上与我在其他对话框中使用的主题相同):

enter image description here


为了快速重现我的问题 -> 我的项目在github上。

你是否考虑过使用自定义视图? - Aiko West
我考虑过,但我不确定这是否是“仅”更改背景颜色和文本颜色的正确方法? - IIIIIIIIIIIIIIIIIIIIII
4个回答

16

回答自己的问题。最终解决方法就是简单地将以下内容替换:

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

随着

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

请添加适当的AppCompat项目。 - Elias Fazel
2
替换它在哪里? - Richard Barraclough

3

我认为您在标记中混淆了事情。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. 自定义警告对话框样式仅限于提供(背景)绘图。

注意-2. 自定义警告对话框主题开启了一种提供属性的方法,如windowBackground、windowTitleStyle等,但您需要一个支持alertDialogTheme属性/主题项目的Android版本。

您回答的解释: 如果从android:alertDialogTheme中删除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

如果您在回答中解释为什么要使用alertDialogTheme而不是android:alertDialogTheme,我将奖励您赏金。 - IIIIIIIIIIIIIIIIIIIIII
谢谢更新,我知道我在这里混淆了一些东西。但我的问题的根本原因是我不清楚为什么需要从android:alertDialogTheme中删除android:(就像我的答案中所示)。您能否指向一些描述此内容的文档? - IIIIIIIIIIIIIIIIIIIIII

1

styles.xml中使用此代码,并将其添加到基础主题中。此样式将警告对话框替换为材料对话框。

    <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>

-2

根据你将要调用这个函数的次数/场合,你可以创建一个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,然后在类内部使用onCreateView而不是onCreateDialog

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

你建议创建一个自定义的ListPreference,覆盖onCreateDialog方法来设置DialogStyle? - IIIIIIIIIIIIIIIIIIIIII
你写道 取决于你会调用多少次/地方。你能描述一下为什么调用的次数/地方很重要吗? - IIIIIIIIIIIIIIIIIIIIII
  1. 我更喜欢使用DialogFragment而不是ListPreference,因为ListPreference不能提供足够的自定义选项来满足我的需求。
  2. DialogFragment提供了更灵活的对话框变化方式,你可以通过传递不同的数据到片段来重用对话框。当然,如果你只使用一个对话框,这并不会产生影响。
- Jacky
但是我不能在PreferenceScreen中使用DialogFragment,你的解决方案对我没有帮助 :/ - IIIIIIIIIIIIIIIIIIIIII

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接