Android 如何在对话框中设置相等的按钮高度?
我有一个进度对话框:Android 如何在对话框中设置相等的按钮高度?,android,android-layout,button,progressdialog,Android,Android Layout,Button,Progressdialog,我有一个进度对话框: mProgressDialog = new ProgressDialog(WebViewActivity.this); 使用以下按钮: mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(Dialog
mProgressDialog = new ProgressDialog(WebViewActivity.this);
使用以下按钮:
mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//some code
}
});
我试图通过(并获取NullPointerException)设置它们的参数:
那么,如何在这里设置按钮的布局参数(不使用自定义进度对话框)?
有些事情告诉我,我根本无法设置它们,而不是使用自定义进度对话框。。。
注意:如果我在这里遗漏了一些非常简单的内容,请原谅按钮安装在dialog.onCreate()之后的对话框中。因此,您可以重写此方法并在此处添加代码,或者必须在调用Dialog.show()后获取该按钮 这是我的样品,我改变了第一个按钮的重量
ProgressDialog dialog = new ProgressDialog(this);
dialog.setButton(AlertDialog.BUTTON_POSITIVE, "confirm",
(DialogInterface.OnClickListener) null);
dialog.setButton(AlertDialog.BUTTON_NEGATIVE, "cancel",
(DialogInterface.OnClickListener) null);
dialog.show();
Button btConfirm = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) btConfirm
.getLayoutParams();
params.weight = 3;
下面是AlertDialog与生成器的代码
AlertDialog.Builder builder = new AlertDialog.Builder(Setup.this);
builder.setMessage("Message", 1))
.setPositiveButton(getString(R.string.browseonlinesupport), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
})
.setNeutralButton(getString(R.string.submit_help_request), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setNegativeButton(getString(R.string.cancel), null) // to make the button even widths
//.show()
;
// }
AlertDialog helpDialog = builder.create();
helpDialog.show(); // call show() must from dialog not builder, otherwise button not created and getButton is null
LinearLayout.LayoutParams btParams;
Button btPositive = helpDialog.getButton(AlertDialog.BUTTON_POSITIVE);
btParams = (LinearLayout.LayoutParams) btPositive.getLayoutParams();
btParams.weight = 1;
btParams.width = btParams.MATCH_PARENT;
Button btNegative = helpDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
btParams = (LinearLayout.LayoutParams) btNegative.getLayoutParams();
btParams.weight = 1;
btParams.width = btParams.MATCH_PARENT;
Button btNeutral = helpDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
btParams = (LinearLayout.LayoutParams) btNeutral.getLayoutParams();
btParams.weight = 1;
btParams.width = btParams.MATCH_PARENT;
*,必须从dialog not builder调用show(),否则按钮未创建且getButton为空您没有将
按钮设置为负
到mProgressDialog
,这就是为什么您会得到nullPointerException
您确实缺少一些简单的东西。您将按钮设置为中性,并尝试将按钮设置为负数。对不起,这是一个输入错误,因为我没有粘贴所有代码。我已经修复了它,当然我的源代码中没有它。也许你能看到这里有什么不对劲?@Houcine也许你注意到这里有什么不对劲?@Aswin Kumar也许你注意到这里有什么不对劲?谢谢!这是我的错误:“您必须在调用Dialog.show()后获取按钮”。
AlertDialog.Builder builder = new AlertDialog.Builder(Setup.this);
builder.setMessage("Message", 1))
.setPositiveButton(getString(R.string.browseonlinesupport), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
})
.setNeutralButton(getString(R.string.submit_help_request), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setNegativeButton(getString(R.string.cancel), null) // to make the button even widths
//.show()
;
// }
AlertDialog helpDialog = builder.create();
helpDialog.show(); // call show() must from dialog not builder, otherwise button not created and getButton is null
LinearLayout.LayoutParams btParams;
Button btPositive = helpDialog.getButton(AlertDialog.BUTTON_POSITIVE);
btParams = (LinearLayout.LayoutParams) btPositive.getLayoutParams();
btParams.weight = 1;
btParams.width = btParams.MATCH_PARENT;
Button btNegative = helpDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
btParams = (LinearLayout.LayoutParams) btNegative.getLayoutParams();
btParams.weight = 1;
btParams.width = btParams.MATCH_PARENT;
Button btNeutral = helpDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
btParams = (LinearLayout.LayoutParams) btNeutral.getLayoutParams();
btParams.weight = 1;
btParams.width = btParams.MATCH_PARENT;