如何垂直堆叠AlertDialog按钮?

时间:2016-03-08 23:47:47

标签: java android

我正在使用构建器在Android中使用模式创建AlertDialogs

AlertDialog.Builder builder = new AlertDialog.Builder(context);

builder.setTitle(...);
builder.setMessage(...);

builder.setPositiveButton(button1Text, ...);
builder.setNeutralButton(button2Text, ...);
builder.setNegativeButton(button3Text, ...);

builder.show();

目前,只显示两个按钮,因为按钮太宽而无法放入对话框中。如何强制按钮垂直堆叠?

我正在使用Theme.AppCompat.Dialog.Alert主题,该主题使用ButtonBarLayout来构建按钮。根据{{​​3}},ButtonBarLayout可以在设置mAllowStacking属性时自动垂直堆叠宽按钮,但在我的情况下似乎默认为false。有没有办法在构建AlertDialog时将其设置为true?

4 个答案:

答案 0 :(得分:4)

我不喜欢黑客。但这立即使我震惊。

  

如果按钮文本太长而无法全部水平放置,则它将   自动排成三个按钮的垂直列。

只需将按钮文本加长即可。

[IS-HOME]/repository

答案 1 :(得分:3)

如果您将警报框作为列表进行操作会怎样?

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.pick_color)
           .setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
               // The 'which' argument contains the index position
               // of the selected item
           }
    });
    return builder.create();
}

从这里取得的示例(在添加列表下):https://developer.android.com/guide/topics/ui/dialogs.html

然后只需获取这些列表选项并将其转换为您想要的内容。

答案 2 :(得分:2)

您无法使用AlertDialog执行此操作。您应该创建自定义Dialog,并自己实现。像这样的东西会这样做

Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
dialog.setTitle(...);
dialog.setMessage(...);
dialog.show();

您的布局dialog_layout.xml应该是

<LinearLayout android:layout_width="match_parent"
              android:layout_height="wrap_content"
              orientation="vertical">

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"/>
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"/>
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>

答案 3 :(得分:0)

这也是MaterialAlertDialog的问题。

我尝试了许多解决方案,但是我认为最好的解决方案是覆盖材质对话框使用的按钮布局。从材料库中复制粘贴mtrl_alert_dialog_actions.xml的内容,然后进行必要的更改。例如,将ButtonBarLayout更改为垂直方向的LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/buttonPanel"
    style="?attr/buttonBarStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:scrollIndicators="top|bottom">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingStart="8dp"
        android:paddingEnd="8dp"
        android:paddingTop="2dp"
        android:paddingBottom="2dp">

        <Button
            android:id="@android:id/button1"
            style="?attr/buttonBarPositiveButtonStyle"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:layout_height="wrap_content"/>

        <Button
            android:id="@android:id/button2"
            style="?attr/buttonBarNegativeButtonStyle"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:layout_height="wrap_content"/>

        <Button
            android:id="@android:id/button3"
            style="?attr/buttonBarNeutralButtonStyle"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</ScrollView>

然后通过将布局添加到res/values/refs.xml来覆盖布局:

<resources xmlns:tools="http://schemas.android.com/tools">
    <item name="mtrl_alert_dialog_actions" type="layout" tools:override="true">@layout/mtrl_alert_dialog_actions_custom</item>
</resources>