美文网首页
沉浸式状态栏一行代码调用

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

作者: 石野小真人 | 来源:发表于2016-11-18 22:42 被阅读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);
    }
    

    相关文章

      网友评论

          本文标题:沉浸式状态栏一行代码调用

          本文链接:https://www.haomeiwen.com/subject/bcedpttx.html