沉浸式状态栏 (UI)

public static void setTransparent(Activity activity){
    if(Build.VERSION.SDK_INT< Build.VERSION_CODES.KITKAT){
        return;
    }
    transparentStatusBar(activity);
    setRootView(activity);
}

private static void transparentStatusBar(Activity activity){
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        //设置flag contentView才能延伸到状态栏
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        //状态栏覆盖在contentView上面,设置透明使contentView的背景透出来
        activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
    }else{
        //contentView延伸到状态栏并且设置状态栏颜色透明
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
}

//把布局容器内所有子view全部setFitsSystemWindows(true);
private static void setRootView(Activity activity){
    ViewGroup parent =(ViewGroup)activity.findViewById(android.R.id.content);
    for(int i=0,count = parent.getChildCount();i

你可能感兴趣的:(UI)