Can I set FLAG_LAYOUT_NO_LIMITS only for status bar?

前端 未结 13 1678
旧时难觅i
旧时难觅i 2020-12-23 13:32

I need to make transparent status bar. I am using getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS) and it is make status bar as I want. Bu

13条回答
  •  梦毁少年i
    2020-12-23 14:02

    For all those interested in workarounds/hacks because Android API concerning this matter is just awful. Do not use any system window flags, set negative margin to the root of your layout, and then use setStatusBarColor to make your StatusBar transparent.

    statusBarHeight = getResources().getDimensionPixelSize(R.dimen.margin_24dp);
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId != 0) {
        statusBarHeight = getResources().getDimensionPixelSize(resourceId);
    }
    
    FrameLayout.LayoutParams rootParams = (FrameLayout.LayoutParams) binding.getRoot().getLayoutParams();
    rootParams.topMargin = -statusBarHeight;
    binding.getRoot().setLayoutParams(rootParams);
    
    getWindow().setStatusBarColor(getResources().getColor(R.color.transparent));
    

    where binding.getRoot() is of course the root of your layout

    and result is

提交回复
热议问题