美文网首页
BottomSheetDialog 沉浸式导航栏不生效

BottomSheetDialog 沉浸式导航栏不生效

作者: lpz0715 | 来源:发表于2023-12-19 18:00 被阅读0次

    使用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);
        }
    }
    

    对比效果:


    image.png

    参考链接:

    https://stackoverflow.com/questions/47553936/prevent-bottomsheetdialogfragment-covering-navigation-bar
    

    相关文章

      网友评论

          本文标题:BottomSheetDialog 沉浸式导航栏不生效

          本文链接:https://www.haomeiwen.com/subject/ufozgdtx.html