繁体   English   中英

Android AlertDialog 将按钮对齐到底部不起作用

[英]Android AlertDialog align buttons to bottom not working

我有一个带有一些信息以及中性和正面按钮的 Android AlertDialog

我使用以下行设置AlertDialog的大小,这AlertDialog提供了我想要的正确高度:

MaterialAlertDialogBuilder alert = new MaterialAlertDialogBuilder(context);
alert.setView(wifiResultView);
// stuff
alertDialog = alert.show();
alertDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

但是,按钮与对话框中数据的末尾对齐,我一生都无法弄清楚如何让它们粘在底部。 有人有任何想法吗? 我将不胜感激。

我希望按钮粘在窗口的底部,数据和按钮之间有空间(而不是像图片中的按钮下方有空间)。

我使用的是 Android 11(三星 Galaxy S10e)

在此处输入图片说明

谢谢一堆!

我正在使用此代码并且它工作正常:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("test title");
    builder.setCancelable(false);
    builder.setPositiveButton("unfreeze", (dialog, which) -> {
        // do some things
    });
    builder.setNeutralButton("ok", (dialog, which) -> {
        // do some things
    });
    AlertDialog alertDialog = builder.create();
    alertDialog.getWindow()
            .setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);

    alertDialog.show();

能否请您分享更多有关您的 AlertDialog 代码和您设备的 Android 版本的详细信息,并尝试在另一部手机中进行测试。

编辑

您可以将Space放在wifiResultView

<Space
        android:layout_width="match_parent"
        android:layout_height="200dp" />

比将LayoutParams.MATCH_PARENT更改为LayoutParams.WRAP_CONTENT

您的代码将是这样的:

alertDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
 ViewGroup.LayoutParams.WRAP_CONTENT);

这是AlertDialog的默认行为。 要将按钮与底部对齐,您还必须将AlertDialogLayout的高度(警报容器)更改为MATCH_PARENT 不幸的是,目前没有一个公共的API来获取AlertDialogLayoutAlertDialog ,所以下面我将介绍两种可能的解决方案,从检索AlertDialog并改变其高度。

1. 直接从 PositiveButton 中找到 AlertDialogLayout:

Button btn = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
AlertDialogLayout alertDialogLayout = (AlertDialogLayout) btn.getParent().getParent().getParent();
alertDialogLayout.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

2.从 PositiveButton 根视图开始以循环方式查找 AlertDialogLayout:

Button btn = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
View rootView = btn.getRootView();
findAlertDialogLayoutAndSetParams(rootView);

其中findAlertDialogLayoutAndSetParams(View view)是一个辅助函数,它会重复出现,直到找到 AlertDialogLayout:

private void findAlertDialogLayoutAndSetParams(View view){

    if(view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup)view;
        for(int i = 0; i < viewGroup.getChildCount(); i++) {
            View childView = viewGroup.getChildAt(i);
            if(childView instanceof ViewGroup) {
                if(childView instanceof AlertDialogLayout){
                    AlertDialogLayout alertDialogLayout = (AlertDialogLayout)childView;
                    alertDialogLayout.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
                }
                else {
                    findAlertDialogLayoutAndSetParams(childView);
                }
            }
        }
    }
}

完整示例:

//get your custom view
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View wifiResultView = mInflater.inflate(R.layout.alert_view_layout, null, false);

//create the MaterialAlertDialogBuilder
MaterialAlertDialogBuilder alert = new MaterialAlertDialogBuilder(context);
alert.setPositiveButton("Ok", null);
alert.setNeutralButton("Cancel", null);
alert.setView(wifiResultView);
AlertDialog alertDialog = alert.show();
alertDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

//stick the buttons to the bottom by setting the height of AlertDialogLayout to MATCH_PARENT
//1st option - get the AlertDialogLayout directly from the positive button
Button btn = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
AlertDialogLayout alertDialogLayout = (AlertDialogLayout) btn.getParent().getParent().getParent();
alertDialogLayout.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

//2nd option - find AlertDialogLayout in a recurring way starting from the positive button root view
//Button btn = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
//View rootView = btn.getRootView();
//findAlertDialogLayoutAndSetParams(rootView);

之前/之后的结果:

button_before button_after

暂无
暂无

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

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