沉浸式状态栏一行代码调用

2016-11-18  本文已影响86人  石野小真人

用到的库

 //状态栏变色
 compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'

方法封装

 /**
 * 需要在setContentview之后再调用
 * @param activity
 */
public static void setStatusBarColor(Activity activity) {
    setStatusBarColor(activity,R.color.base);
}

/**
 * 需要在setContentview之后再调用
 * @param activity
 */
public static void setStatusBarColor(Activity activity,int colorRes) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        setTranslucentStatus(activity, true);
        SystemBarTintManager mTintManager = new SystemBarTintManager(activity);
        mTintManager.setStatusBarTintEnabled(true);
        mTintManager.setStatusBarTintResource(colorRes);
    }
}

public static void setStatusBarTranslucent(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        setTranslucentStatus(activity, true);
        //activity.getWindow().getDecorView().
    }
}


@TargetApi(19)
private static void setTranslucentStatus(Activity activity, boolean on) {
    Window win = activity.getWindow();
    WindowManager.LayoutParams winParams = win.getAttributes();
    final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
    if (on) {
        winParams.flags |= bits;
    } else {
        winParams.flags &= ~bits;
    }
    win.setAttributes(winParams);
}
上一篇下一篇

猜你喜欢

热点阅读