禁用半透明状态栏后的 DrawerLayout 神器

DrawerLayout artifact after disable translucent status bar

首先启用半透明状态栏

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);  

禁用后显示奇怪的工件(半透明线),如图

UPD github

上的示例项目

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

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 时填充 window 并将在状态栏上方绘制。之后需要为子视图设置填充和边距