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

问题描述 投票:11回答:3

我试图改变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

android android-alertdialog listpreference
3个回答
5
投票

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

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

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

0
投票

我认为你正在混合标记中的东西。 alertDialogStyle和alertDialogTheme都不同。

自定义警告对话框主题,你应该创建一个Dialog主题,一个应该扩展@deroid的主题: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。自定义警报对话框样式仅限于提供(背景)drawable。

笔记2。自定义警报对话框主题会打开一个方法来提供诸如windowBackground,windowTitleStyle等属性,但是您需要一个Android版本,它支持主题的alertDialogThem属性/项目

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

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

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

问题参考:qazxsw poi

变更参考:qazxsw poi


-1
投票

根据您将这个调用多少时间/地点,您可以创建一个DialogFragment来处理这个问题。

DialogFragment可以很简单:

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之后添加以下内容:

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
© www.soinside.com 2019 - 2024. All rights reserved.