简体   繁体   English

如何将警报对话框按钮向左对齐?

[英]how to align alert Dialog buttons to the left?

I want to create an alert dialog with ltr buttons. 我想用ltr按钮创建一个警告对话框。 but it seems like we can't access positive button details. 但似乎我们无法访问正面的按钮详细信息。

this answer was for aligning buttons to center, but I want to align it to the left. 这个答案是将按钮居中对齐,但我想将其向左对齐。

I just checked stack and didn't find anything that covers this.i don't want to create custom_layout.xml for dialog. 我只是检查了堆栈,却没有发现任何涵盖此内容的内容。我不想为对话框创建custom_layout.xml。

This is the answer from the link you posted. 这是您发布的链接的答案。 You need to change the weight of the layout params from 10 to -10 to go from center to left. 您需要将布局参数的权重从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);

Hope it helps! 希望能帮助到你!

More information: Now that I understand the question, no sadly you cannot change the order or the buttons. 详细信息:既然我已经理解了这个问题,那么很遗憾,您不能更改顺序或按钮。 The order is NEGATIVE - NEUTRAL - POSITIVE. 顺序为负-中性-正。 But once you have the button you can choose whatever to do with. 但是一旦有了按钮,您就可以选择处理任何事情。 For instance, you can use the negative button as the positive, by just renaming it in one line. 例如,您可以将负号按钮用作正号,只需将其重命名为一行即可。

You can try this ; 您可以尝试一下;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM