java - Android AlertDialog 将按钮对齐到底部不起作用
问题描述
我有一个AlertDialog
带有一些信息和 Neutral 和 Positive 按钮的 Android。
我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
。AlertDialogLayout
要将按钮与底部对齐,您还必须将警报容器的高度更改为MATCH_PARENT
. 不幸的是,目前没有公共 API 来检索 AlertDialogLayout
from AlertDialog
,所以下面我将描述两种可能的解决方案来检索它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);
之前/之后的结果:
推荐阅读
- selenium - 如何在xpath中匹配正则表达式
- python - ModuleNotFoundError:没有名为“apps.news”的模块;'apps' 不是一个包
- html - 检查html中的空角度对象
- autohotkey - Shift,Ctrl,Win 拦截我的自动点击热键
- java - 获取 JschException 的原始原因
- java - 已创建客户端时无法使用 Jersey 2.x 修改身份验证凭据
- kubernetes - 使用 calico 网络处于 Crashloopbackoff 状态的 Coredns
- flutter - Flutter:按钮应该输出一个小部件,但没有
- kubernetes - Kubernetes RBAC 是如何对服务账户实施的?
- reactjs - 有条件地在子组件上渲染父组件屏幕的表单 onSubmit() 调用。(w/react-redux)