AlertDialog 按钮未包装在 Lollipop 中

AlertDialog Buttons not wrapping in Lollipop

我是一个相对较新的 Android 开发人员并且 运行 遇到了一个我真的不知道如何纠正的问题,希望你们能够提供建议!

在 Kitkat 及以下版本中,当我创建一个具有 2 个按钮的 AlertDialog 时,如果两个按钮的文本超出按钮的长度,它们的文本就会换行。但是,在 L 中,文本不适合。

Android 4.4:

Android大号:

我发现了其他一些人遇到此问题并寻求解决方案的示例,因此我开始研究为他们提供的解决方案

Alert dialog buttons problems in Android L

https://github.com/hotchemi/Android-Rate/issues/40

根据回答,结论似乎是警报对话框的自定义样式。

我创建了一个 values-v21 文件夹,里面有一个 styles.xml。从上面看起来像下面这样:

<resources>      

    <style name="AppBaseTheme" parent="android:Theme.Material.Light">
        <item name="android:alertDialogTheme">@style/CustomAlertDialogStyle</item>
    </style>

    <style name="CustomAlertDialogStyle" parent="android:Theme.Material.Light.Dialog.Alert">
        <item name="android:buttonBarButtonStyle">@style/CustomButtonBarButtonStyle</item>
        <item name="android:buttonBarStyle">@style/CustomButtonBarStyle</item>
    </style>

    <style name="CustomButtonBarStyle" parent="@android:style/Widget.Material.Light.ButtonBar.AlertDialog">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:height">@null</item>
        <item name="android:minHeight">@null</item>
    </style>

    <style name="CustomButtonBarButtonStyle" parent="@android:style/Widget.Material.Light.Button.Borderless.Colored">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_weight">1</item>
    </style>

</resources>

不幸的是,我在 lollipop Alertdialog 中没有看到任何明显的变化,而且我对布局值仍然不太熟悉,无法确定可能有什么问题。

所以在摸索了一会儿之后,我想我会问:有谁知道如何制作类似于 Android 4.4 的 Android L 按钮文本自动换行?

谢谢!

如前所述,新的设计指南明确规定对话框应具有清晰简洁的文本,以便用户快速做出决定。因此不鼓励长期行动。

但是,从 Lollipop 开始,允许使用新的设计模式来容纳更大的文本。查看 here 并向下滚动到 Stacked full-width buttons。基本上,您可以将按钮堆叠在一起,并使用完整的对话框宽度。它不能解决您的问题,因为您仍然受限于全宽,但可以提供帮助。

至于如何去做,我认为AlertDialogclass没有提供这方面的简单方法。几点建议:

  • 忘记 positivenegative 按钮,只需为完全没有按钮的对话框设置自定义视图。在该视图中,您可以拥有标题、消息 所有您想要的按钮。
  • 使用这个很棒的 library,它可以让您的对话框在旧设备上焕然一新,并且有一个名为 forceStacking() 的便捷方法,您可以阅读。实现起来非常简单。

我也遇到了类似的问题。我正在使用 android.app.AlertDiaglog。刚刚改变了 导入到 android.support.v7.app.AlertDialog。

支持导入发挥了作用。