Android AlertDialog 将按钮底部对齐不起作用
Android AlertDialog align buttons to bottom not working
我有一个 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);
但是,按钮与对话框中数据的末尾对齐,我一辈子都想不出如何让它们粘在底部。有人知道吗?我将不胜感激。
我希望按钮粘在 window 的底部,数据和按钮之间有 space(而不是像图片中那样在按钮下方有 space) .
我正在使用 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 版本的详细信息,同时尝试在花药 phone.
中进行测试
编辑
你可以把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
。不幸的是,目前没有 public API 从 AlertDialog
检索 AlertDialogLayout
,所以下面我将描述两种可能的解决方案,从 AlertDialog
检索它并更改它的高度。
1.Find 直接来自 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.Find AlertDialogLayout 从 PositiveButton 根视图开始以循环方式出现:
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);
结果before/after:
我有一个 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);
但是,按钮与对话框中数据的末尾对齐,我一辈子都想不出如何让它们粘在底部。有人知道吗?我将不胜感激。
我希望按钮粘在 window 的底部,数据和按钮之间有 space(而不是像图片中那样在按钮下方有 space) .
我正在使用 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 版本的详细信息,同时尝试在花药 phone.
中进行测试编辑
你可以把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
。不幸的是,目前没有 public API 从 AlertDialog
检索 AlertDialogLayout
,所以下面我将描述两种可能的解决方案,从 AlertDialog
检索它并更改它的高度。
1.Find 直接来自 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.Find AlertDialogLayout 从 PositiveButton 根视图开始以循环方式出现:
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);
结果before/after: