今天我正在尝试新的材料组件,它们的安装部分是您需要更改应用程序的父级以从 Theme.MaterialComponents 继承。所以我这样做是因为我想使用具有更好波纹的底部导航。但在那之后,应用程序中的几乎所有按钮都变得更加臃肿。
我应该怎么做才能恢复到以前的状态(右图)?
今天我正在尝试新的材料组件,它们的安装部分是您需要更改应用程序的父级以从 Theme.MaterialComponents 继承。所以我这样做是因为我想使用具有更好波纹的底部导航。但在那之后,应用程序中的几乎所有按钮都变得更加臃肿。
我应该怎么做才能恢复到以前的状态(右图)?
在研究过程中,我找到了答案,我会把它留在这里,也许它会对某人有所帮助。
它们看起来像这样的原因是因为它们使用style="attr/buttonBarNegativeButtonStyle"
并且 Material 主题会覆盖它们
要解决此问题,您需要使用 Bridge 主题而不是Theme.MaterialComponents.Light
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.Bridge">
<!-- ... -->
</style>
另一种方法是使用AlertDialog
AndroidX AppCompat 库:
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();
注意:如果您正在使用Android 库的 Material Components和/或您正在使用新的Theme.MaterialComponents.*
主题,您应该使用MaterialAlertDialogBuilder
class,它应该用来代替 AppCompatAlertDialog
类,如下所述:
Material 维护了 framework 的使用
AlertDialog
,但提供了一个新的 builder ,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()
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
等