【发布时间】:2019-02-16 23:14:16
【问题描述】:
今天我正在尝试新的材料组件,其中一部分是您需要将应用的父级更改为从 Theme.MaterialComponents 继承。 所以我这样做是因为我想使用具有更好波纹的底部导航。但在那之后,应用程序中的几乎所有按钮都变得更加臃肿。
我应该怎么做才能恢复到以前的状态(右图)?
【问题讨论】:
标签: android material-components-android
今天我正在尝试新的材料组件,其中一部分是您需要将应用的父级更改为从 Theme.MaterialComponents 继承。 所以我这样做是因为我想使用具有更好波纹的底部导航。但在那之后,应用程序中的几乎所有按钮都变得更加臃肿。
我应该怎么做才能恢复到以前的状态(右图)?
【问题讨论】:
标签: android material-components-android
在研究过程中,我找到了答案,我将把它留在这里,也许它会对某人有所帮助。
它们看起来像这样的原因是因为它们使用 style="attr/buttonBarNegativeButtonStyle" 并且 Material 主题会覆盖它们
要解决此问题,您需要使用 Bridge 主题而不是 Theme.MaterialComponents.Light
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.Bridge">
<!-- ... -->
</style>
【讨论】:
另一种方法是使用 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()
【讨论】:
MaterialAlertDialogBuilder 类:github.com/material-components/material-components-android/blob/…
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>
但是当你使用ExtendedFloatingActionButton、MaterialButton等材质组件时会导致你
【讨论】: