Material 更改应用程序主题后,组件主题对话框按钮变得浮肿

Material Components theme dialog buttons go puffy after changing theme of Application

今天我正在尝试新的 material 组件,安装的一部分是您需要更改应用程序的父级以继承自 Theme.MaterialComponents。 所以我这样做是因为我想使用波纹效果更好的底部导航。但在那之后,应用程序中的几乎所有按钮都变得更加蓬松。

我应该怎么做才能恢复到以前的状态(右图)?

左边是material版本右边是appcompat版本

在研究过程中,我找到了答案我会把它留在这里也许它会对某人有所帮助。

它们看起来像这样的原因是因为它们使用 style="attr/buttonBarNegativeButtonStyle" 并且 Material 主题覆盖了它们

要解决此问题,您需要使用 Bridge 主题而不是 Theme.MaterialComponents.Light

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.Bridge">
    <!-- ... -->
</style>

更多信息: https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md#bridge-themes

另一种方法是使用 AndroidX AppCompat 库中的AlertDialog

AlertDialog signInDialog = new AlertDialog.Builder(this)
    .setMessage("Are you sure you want to log out?")
    .setPositiveButton("OK", (dialogInterface, i) -> {
        // TODO: Add your code here
    })
    .setNegativeButton("Cancel", (dialogInterface, i) -> {
        // TODO: Add your code here
    })
signInDialog.show();

注意:如果您使用的是 Material Components for Android library and/or you're using the new Theme.MaterialComponents.* themes, you should instead use the MaterialAlertDialogBuilder class,应使用它代替 AppCompat AlertDialog class,如下所述:

Material maintains usage of the framework AlertDialog, but provides a new builder, MaterialAlertDialogBuilder, which configures the instantiated AlertDialog with Material specs and theming.

这是一个例子(在 Kotlin 中):

MaterialAlertDialogBuilder(this).apply {
    setMessage("Are you sure you want to log out?")
    setPositiveButton("OK") {
        TODO("Unimplemented functionality")
    }
    setNegativeButton("Cancel") {
        TODO("Unimplemented functionality")
    }
}.show()

Solution

停止使用 android.app.AlertDialog 并替换为 androidx.appcompat.app.AlertDialog 它将解决您的问题

You can use

    <style name="AppTheme" parent="@style/Theme.MaterialComponents.Light.Bridge">
    </style>

    or

    <style name="AppTheme" parent="@style/Theme.MaterialComponents.Light.NoActionBar.Bridge">
    </style>       

但是当你使用material组件如ExtendedFloatingActionButtonMaterialButton

时,它会导致你