根据版本的不同,警报对话框和对话框的外观也有所不同。
在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文件夹
将AlertDialog
与Theme.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>