我有一个自定义Dialog
,它可以正常工作,但是有没有办法让对话框适合屏幕?因此可以看到header
(在下图中的橙色)。而且不会有黑色的一面,这意味着对话框基本上会覆盖整个view
,但不会覆盖header
?
您可以按照以下代码设置自定义对话框窗口的重力。
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher_background);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
请注意:仅当您的内容不与AppBar重叠时,此方法才有效。如果您的内容与AppBar重叠,则必须检查替代方法。我会在这里建议片段。
您是否尝试过使用AndroidX AlertDialog?另外,UX方面,为什么对话框视图上有一个后退按钮?