如何在进度对话框中设置相等的按钮高度?

时间:2012-12-11 14:52:20

标签: android android-layout button progressdialog

我有进度对话框:

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)来设置它们的参数:

Button cancel = mProgressDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
int width1 = (int) TypedValue.applyDimension
                (TypedValue.COMPLEX_UNIT_DIP, 0, getResources().getDisplayMetrics());
         **NullPointerException here:** cancel.setLayoutParams(new LinearLayout.LayoutParams
                 (width1, LayoutParams.MATCH_PARENT, 1f));

那么,如何在此处设置按钮的布局参数(不使用自定义进度对话框)? 有些东西告诉我,我根本无法设置它们,而不是使用自定义进度对话框...

P.S。:抱歉,如果我在这里遗漏了一些非常简单的事情

2 个答案:

答案 0 :(得分:1)

在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;

答案 1 :(得分:1)

这里是使用构建器

的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;

*,调用show()必须来自dialog not builder,否则按钮未创建且getButton为null