简体   繁体   English

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

[英]Android - Default Alert Dialog Button position to the bottom of the Dialog

Is it possible to control the position of the Button in the default alert dialog? 是否可以控制按钮在默认警报对话框中的位置?

I am trying to position the button to the bottom of the window. 我试图将按钮放置在窗口底部。

警报对话框的屏幕快照

Bigger the device, the position of the Button is off from the bottom margin of the Alert dialog. 设备越大,按钮的位置就越远离“警报”对话框的底部边缘。

Tried a couple of different options but it's not working. 尝试了几种不同的选择,但是没有用。

I am also not trying to use a custom dialog and just use the default alert dialog box. 我也不想使用自定义对话框,而只使用默认的警报对话框。

Here is my code : 这是我的代码:

 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);
}

Use this to set the padding of the buttons: 使用此设置按钮的填充:

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();
        }
    }
}

Change 0 padding to what you like. 将0填充更改为所需的填充。

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

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