用于自定义状态栏的FLAG_LAYOUT_NO_LIMITS使导航栏重叠

时间:2018-11-11 16:51:28

标签: android android-windowmanager

我搜索了为状态栏设置可绘制对象的解决方案,我发现了这一点:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window window = activity.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }

此代码可以正常工作,但也会带来另一个问题! 如果用户的android设备具有底部导航按钮(软件按钮),则我的布局会落后于它!当然,这个问题也发生在我的布局顶部。 You can see it here

在其他项目与我的布局重叠时没有问题时,是否可以使用自定义状态栏?

1 个答案:

答案 0 :(得分:0)

这段代码解决了我的问题!

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    //make full transparent statusBar
    if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
        setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true);
    }
    if (Build.VERSION.SDK_INT >= 19) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }
    if (Build.VERSION.SDK_INT >= 21) {
        setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
    }
}

public static void setWindowFlag(Activity activity, final int bits, boolean on) {
    Window win = activity.getWindow();
    WindowManager.LayoutParams winParams = win.getAttributes();
    if (on) {
        winParams.flags |= bits;
    } else {
        winParams.flags &= ~bits;
    }
    win.setAttributes(winParams);
}