在 BottomSheetDialog 设置高度时,不能使用 MATCH_PARENT,因为它将覆盖整个屏幕,而应该使用 WRAP_CONTENT 或特定高度,以适当地显示 BottomSheetDialog 的内容。示例代码如下:
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(context);
View bottomSheetView = LayoutInflater.from(context).inflate(R.layout.layout_bottom_sheet, null);
bottomSheetDialog.setContentView(bottomSheetView);
// 动态设置 BottomSheetDialog 的高度
bottomSheetView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
bottomSheetView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int height = bottomSheetView.getHeight();
if (height > 0) {
// 设置 BottomSheetDialog 的高度
bottomSheetDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, height);
}
}
});
bottomSheetDialog.show();
上一篇:AndroidBottomSheetBehavior在ViewPager2中无论是否折叠都会占据所有高度。
下一篇:AndroidBottomSheetDialogFragment设定visibility为Gone后会被弹回最上方的问题