【问题标题】:Material Components theme dialog buttons go puffy after changing theme of Application更改应用程序主题后,材质组件主题对话框按钮变得浮肿
【发布时间】:2019-02-16 23:14:16
【问题描述】:

今天我正在尝试新的材料组件,其中一部分是您需要将应用的父级更改为从 Theme.MaterialComponents 继承。 所以我这样做是因为我想使用具有更好波纹的底部导航。但在那之后,应用程序中的几乎所有按钮都变得更加臃肿。

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

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

【问题讨论】:

    标签: android material-components-android


    【解决方案1】:

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

    它们看起来像这样的原因是因为它们使用 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

    【讨论】:

    • 这对我有帮助,但没有真正的解释为什么。谷歌的问题跟踪器中也有一个开放的错误(issuetracker.google.com/issues/116861837),所以它似乎是一个错误。
    • 这行得通,但使用桥接也可以消除使用 TextButton 时按钮上的涟漪效应,但不知道如何解决这个问题!
    【解决方案2】:

    另一种方法是使用 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 和/或您正在使用新的 Theme.MaterialComponents.* 主题,您应该使用 MaterialAlertDialogBuilder class,它应该用来代替 AppCompat AlertDialog 类如下所述:

    Material 保持对框架 AlertDialog 的使用,但提供了一个新的构建器 MaterialAlertDialogBuilder,它使用 Material 规范和主题配置实例化的 AlertDialog。

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

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

    【讨论】:

    【解决方案3】:

    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>       
    

    但是当你使用ExtendedFloatingActionButtonMaterialButton等材质组件时会导致你

    【讨论】:

      猜你喜欢
      • 2019-06-03
      • 2021-08-14
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多