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

标签 java android android-alertdialog

我有一个 Android 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)

enter image description here

非常感谢!

最佳答案

这是AlertDialog的默认行为。要将按钮与底部对齐,您还必须将警报容器 AlertDialogLayout 的高度更改为 MATCH_PARENT。不幸的是,目前没有公共(public) API 可以从 AlertDialog 检索 AlertDialogLayout,因此下面我将描述两种可能的解决方案,从 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 Root View 开始,以循环方式找到 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);

前后结果:

buttons_before buttons_after

关于java - Android AlertDialog 将按钮对齐到底部不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69557222/

相关文章:

java - 防止使用无效参数构造实例

java - 对 .split() 在 Java 中的工作方式感到困惑

java - 从 BroadcastReceiver 对象访问类

android - 错误 :Execution failed for task ':app:transformClassesWithJarMergingForDebug' on facebook accesstoken. 类

java - 如何在 fragment 中显示 AlertDialog?

android - 如何使用字符串中的内容制作对话框?

java - 如何从 .jks 获取 .crt 文件

android - 如何检测点击事件是否在android中完成?

android - 在 Android 设备上启动应用程序时出现黑屏

android - 单击后退按钮后如何关闭警报框