BottomSheetDialog 沉浸式导航栏不生效
2023-12-19 本文已影响0人
lpz0715
使用BottomSheetDialog 时底部导航栏、手势条部分颜色会显示变暗,影响视觉体验
试了各种沉浸式方法,都没办法生效
此方法用 LayerDrawable 替换了窗口背景,该 LayerDrawable 包含两个元素:背景暗淡和导航栏背景
@RequiresApi(api = Build.VERSION_CODES.M)
private void setWhiteNavigationBar(@NonNull Dialog dialog) {
Window window = dialog.getWindow();
if (window != null) {
DisplayMetrics metrics = new DisplayMetrics();
window.getWindowManager().getDefaultDisplay().getMetrics(metrics);
GradientDrawable dimDrawable = new GradientDrawable();
// ...customize your dim effect here
GradientDrawable navigationBarDrawable = new GradientDrawable();
navigationBarDrawable.setShape(GradientDrawable.RECTANGLE);
navigationBarDrawable.setColor(Color.WHITE);
Drawable[] layers = {dimDrawable, navigationBarDrawable};
LayerDrawable windowBackground = new LayerDrawable(layers);
windowBackground.setLayerInsetTop(1, metrics.heightPixels);
window.setBackgroundDrawable(windowBackground);
}
}
对比效果:

参考链接:
https://stackoverflow.com/questions/47553936/prevent-bottomsheetdialogfragment-covering-navigation-bar