Android:Alert和AlertDialog上的主题更改取决于Android版本

问题描述 投票:0回答:1

根据版本的不同,警报对话框和对话框的外观也有所不同。

在styles.xml,我们有以下内容

<style name="Theme.AppSomething" parent="Theme.MaterialComponents.Light.DarkActionBar">

    <item name="alertDialogTheme">@style/Theme.AppSomething.Alert</item>
    <item name="android:alertDialogTheme">@style/Theme.AppSomething.Alert</item>

    <item name="dialogTheme">@style/Theme.Dialog</item>
    <item name="android:dialogTheme">@style/Theme.Dialog</item>

所有AlertDialogs都导入到这样的代码上

    import androidx.appcompat.app.AlertDialog;

现在,删除android:alertDialogTheme非常适用于Android 8(窗口宽度和按钮),但是Android 5的按钮弄乱了。

在Android 5上删除alertDialogTheme,按钮不错,但窗口宽度很小。在Android 8上,宽度再次变小,但按钮却粘在一起。

问题是我不明白为什么它取决于操作系统版本,我应该保留两者中的哪一个,或者我们应该如何修改应用程序以使其具有一定的一致性。

编辑。没有值-v ## / styles.xml文件夹

android android-alertdialog android-theme material-components-android material-components
1个回答
0
投票

AlertDialogTheme.MaterialComponents一起使用的最佳方法是使用MaterialAlertDialogBuilder类:

new MaterialAlertDialogBuilder(this,)
            .setTitle("Dialog")
            ...
            .show();

和主题中的materialAlertDialogTheme属性。

<style name="Theme.AppSomething" parent="Theme.MaterialComponents.Light.DarkActionBar">
    ...
    <item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog</item>
</style>
© www.soinside.com 2019 - 2024. All rights reserved.