如何垂直堆叠AlertDialog按钮?

如何垂直堆叠AlertDialog按钮?

问题描述:

我使用生成器使用模式创造的Android AlertDialogs如何垂直堆叠AlertDialog按钮?

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来构建按钮。根据this answerButtonBarLayout可以自动垂直堆叠宽按钮,当它的mAllowStacking属性被设置,但它似乎默认为false在我的情况。当我构建AlertDialog时,有没有办法将其设置为true?

+1

的Android决定在AlertDialog按钮的位置。如果你需要不同的东西,你将不得不自己实现它。 –

你不能这样做与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> 
+0

我正试图做这样的事情,但无法弄清楚如何从按钮监听器中消除对话框。视图(和监听器)是在构建对话框之前创建的,所以我还没有想出如何解除它。 – MayNotBe

如果将警告框作为列表进行操作,该怎么办?从这里取

@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

然后,只需把这些列表选项,把它们变成你想要的。