Android-默认警报对话框按钮位于对话框底部

时间:2018-08-10 01:23:14

标签: android alertdialog

是否可以控制默认警报对话框中按钮的位置?

我试图将按钮放置在窗口底部。

Alert Dialog Screenshot attached

设备越大,按钮的位置就越远离“警报”对话框的底部。

尝试了两个不同的选项,但是没有用。

我也不想使用自定义对话框,而只是使用默认的警报对话框。

这是我的代码:

 public void showTargetDialog() {

    final NumberPicker numberPicker = new NumberPicker(this);

    numberPicker.setMinValue(1);
    numberPicker.setMaxValue(100);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setMessage("Set target")
            .setPositiveButton("    SET    ", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    int weeklyTarget = numberPicker.getValue() ;
                }
            })
            .setNegativeButton("    CANCEL   ", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // No Action Required
                }
            });

    builder.setView(numberPicker);

    DisplayMetrics metrics = getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    AlertDialog dialog = builder.show();
    dialog.getWindow().setLayout( (int)(0.75 * width), (int) (0.52 * height) );

    Button posButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    Button negButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);

    // NOT WORKING
    posButton.setGravity(Gravity.BOTTOM);

    LinearLayout.LayoutParams layoutParamsPos = (LinearLayout.LayoutParams) posButton.getLayoutParams();
    layoutParamsPos.weight = 1;
    //NOT WORKING
    layoutParamsPos.gravity = Gravity.BOTTOM ;

    LinearLayout.LayoutParams layoutParamsNeg = (LinearLayout.LayoutParams) negButton.getLayoutParams();
    layoutParamsNeg.weight = 1;

    posButton.setLayoutParams(layoutParamsPos);
    negButton.setLayoutParams(layoutParamsNeg);
}

1 个答案:

答案 0 :(得分:0)

使用此按钮设置按钮的填充:

public void showTargetDialog() {

    final NumberPicker numberPicker = new NumberPicker(this);

    numberPicker.setMinValue(1);
    numberPicker.setMaxValue(100);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setMessage("Set target")
            .setPositiveButton("    SET    ", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    int weeklyTarget = numberPicker.getValue() ;
                }
            })
            .setNegativeButton("    CANCEL   ", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // No Action Required
                }
            });

    builder.setView(numberPicker);

    AlertDialog dialog = builder.show();

    int[] buttons = new int[] {AlertDialog.BUTTON_POSITIVE, AlertDialog.BUTTON_NEGATIVE, AlertDialog.BUTTON_NEUTRAL};
    for (int i : buttons) {
        Button b = null;
        try {
            b = dialog.getButton(i);
            b.setPadding(0, 0, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

将0填充更改为您想要的。