禁用半透明状态栏后的DrawerLayout工件

时间:2016-07-22 09:48:54

标签: android navigation-drawer statusbar

首先启用半透明状态栏

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);       
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);  

需要后动态禁用它

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);  

禁用显示的奇怪神器(半透明线)后,如图片

enter image description here

github

上的

UPD 示例项目

1 个答案:

答案 0 :(得分:0)

经过研究,我找到了解决方案并编写了自定义类

public class DrawableLayoutFullScreen extends DrawerLayout {

    public DrawableLayoutFullScreen(Context context) {
        this(context,null);
    }

    public DrawableLayoutFullScreen(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setOnApplyWindowInsetsListener((v, insets) -> {
                final DrawableLayoutFullScreen drawerLayout = (DrawableLayoutFullScreen) v;
                drawerLayout.setWillNotDraw(insets.getSystemWindowInsetTop() <= 0 && getBackground() == null);
                drawerLayout.requestLayout();
                return insets.consumeSystemWindowInsets();
            });
        }
    }

    @Override
    public void setFitsSystemWindows(boolean fitSystemWindows) {
        if (fitSystemWindows) {
            setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        } else {
            setSystemUiVisibility(0);
        }
        super.setFitsSystemWindows(fitSystemWindows);
    }
}

当调用setFitsSystemWindows(true) DrawableLayoutFullScreen填充窗口时,将绘制上面的状态栏。之后需要为子视图设置填充和边距