全宽警告对话框按钮

4
我正在尝试让我的AlertDialogs看起来像这样,带有全宽度按钮:

enter image description here

我正在遵循Google的文档,文档中建议使用以下样式:

 R.style.ThemeOverlay_MaterialComponents_MaterialAlertDialog_FullWidthButtons

但我无法访问该主题:

enter image description here

我已经在项目的Gradle文件中添加了相应的依赖项:

implementation 'com.google.android.material:material:1.5.0'

我感觉我只是错过了一些显而易见的东西。

2个回答

4
主题覆盖的实现在库中缺失。
您可以自己定义它,方法如下:
声明全宽按钮样式,例如:
  <style name="Button.FullWidth" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_weight">1</item>
    <item name="android:maxWidth">@null</item>
  </style>

使用全宽度按钮样式声明主题叠加层:

  <style name="ThemeOverlay.MaterialComponents.MaterialAlertDialog.FullWidthButtons">
    <item name="materialAlertDialogButtonSpacerVisibility">2</item>
    <item name="buttonBarPositiveButtonStyle">@style/Button.FullWidth</item>
    <item name="buttonBarNegativeButtonStyle">@style/Button.FullWidth</item>
    <item name="buttonBarNeutralButtonStyle">@style/Button.FullWidth</item>
  </style>

enter image description here


这个很不错,但是你知道如何在继承“Theme.Material3.Dark.Dialog.Alert”后获取全宽度按钮吗? - Kris B
我发现如果将android:layout_weight设置为10之类的值,它可以工作,但这似乎不是一个合适的解决方案。 - Kris B

0
Gabriel的回答很好。如果你想去掉中间的空格,只需设置
<item name="materialAlertDialogButtonSpacerVisibility">2</item>

在您的警告对话框样式中,GONE 表示已消失。默认值为 1,表示 不可见

是的,但这引出了一个问题,为什么这种风格不在图书馆里呢? - undefined
我知道,我刚刚回答了你在评论中对加布里埃尔回答的问题。 - undefined
这更像是一个修辞性问题,因为只有谷歌才能回答它。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接