【问题标题】:how to align alert Dialog buttons to the left?如何将警报对话框按钮对齐到左侧?
【发布时间】:2019-11-12 19:43:20
【问题描述】:

我想创建一个带有 ltr 按钮的警报对话框。但似乎我们无法访问正面按钮的详细信息。

这个answer 用于将按钮对齐到中心,但我想将它对齐到左侧。

我刚刚检查了堆栈,但没有找到任何内容。我不想为对话框创建 custom_layout.xml。

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:

    这是您发布的链接中的答案。您需要将布局参数的权重从 10 更改为 -10 以从中心向左移动。

     AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
     alertDialog.setTitle("Title");
     alertDialog.setMessage("Message");
    
     alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Yes",
                 new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
    
    alertDialog.show();
    
    Button btnPositive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
    Button btnNegative = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
    
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) btnPositive.getLayoutParams();
    
    //Changing the weight to negative pushes it to the left.
    layoutParams.weight = -10;
    
    btnPositive.setLayoutParams(layoutParams);
    btnNegative.setLayoutParams(layoutParams);
    

    希望对你有帮助!

    更多信息:既然我理解了这个问题,很遗憾你不能更改顺序或按钮。顺序为负 - 中性 - 正。但是一旦你有了按钮,你就可以选择要做什么。例如,您可以将否定按钮用作肯定按钮,只需将其重命名为一行即可。

    【讨论】:

    • 为什么不行,我试过了,效果很好。你尝试过别的吗?
    • 我们只是将按钮推到左边,但是,积极按钮仍然在消极按钮的右边。我应该如何改变他们的位置? @gaurav购物中心
    • 你也应该添加一个中性按钮。
    • @VikaS 还没有尝试过,但如果没有,请寻找更新的帖子。这是一个非常具体的问题的解决方案,所以如果您在较新的帖子中看到通用解决方案会更好。我认为这个答案不值得修改。
    【解决方案2】:

    你可以试试这个;

    new AlertDialog.Builder(activity)
                .setNeutralButton("Your text", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //do what you want
                    }
                }).show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多