【发布时间】:2019-11-12 19:43:20
【问题描述】:
我想创建一个带有 ltr 按钮的警报对话框。但似乎我们无法访问正面按钮的详细信息。
这个answer 用于将按钮对齐到中心,但我想将它对齐到左侧。
我刚刚检查了堆栈,但没有找到任何内容。我不想为对话框创建 custom_layout.xml。
【问题讨论】:
标签: android android-alertdialog
我想创建一个带有 ltr 按钮的警报对话框。但似乎我们无法访问正面按钮的详细信息。
这个answer 用于将按钮对齐到中心,但我想将它对齐到左侧。
我刚刚检查了堆栈,但没有找到任何内容。我不想为对话框创建 custom_layout.xml。
【问题讨论】:
标签: android android-alertdialog
这是您发布的链接中的答案。您需要将布局参数的权重从 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);
希望对你有帮助!
更多信息:既然我理解了这个问题,很遗憾你不能更改顺序或按钮。顺序为负 - 中性 - 正。但是一旦你有了按钮,你就可以选择要做什么。例如,您可以将否定按钮用作肯定按钮,只需将其重命名为一行即可。
【讨论】:
你可以试试这个;
new AlertDialog.Builder(activity)
.setNeutralButton("Your text", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do what you want
}
}).show();
【讨论】: