Android 实现沉浸式(使用SystemBarTint第三方)

来源:互联网 发布:快速选择中值算法 编辑:程序博客网 时间:2024/06/03 08:58
1.导入Jar https://github.com/jgilfelt/SystemBarTint
2.在BaseActivity中配置
protected int mColorId=R.color.statusbar_bg;//状态栏的默认背景色private SystemBarTintManager tintManager; @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        initStateBar();    } /**     * 初始化沉浸式     */    private void initStateBar() {                 setColorId();                if (isNeedLoadStatusBar()) {            loadStateBar();        }    }private void loadStateBar() {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            setTranslucentStatus(true);        }        tintManager = new SystemBarTintManager(this);        // 激活状态栏设置        tintManager.setStatusBarTintEnabled(true);        // 激活导航栏设置        tintManager.setNavigationBarTintEnabled(true);        // 设置一个状态栏颜色        tintManager.setStatusBarTintResource(getColorId());            }

@TargetApi(19)    private void setTranslucentStatus(boolean on) {        Window win = 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);    } /**     * 如果子类使用非默认的StatusBar,就重写此方法,传入布局的id     */    protected void setColorId() {        //this.mColorId=R.color.XXX;子类重写方式    }    protected int getColorId() {        return mColorId;    }    /**     * 子类是否需要实现沉浸式,默认需要     *     * @return     */    protected boolean isNeedLoadStatusBar() {        return true;    }

3.在布局文件根标签加入下面两行代码

android:clipToPadding="false"android:fitsSystemWindows="true"
阅读全文
0 0
原创粉丝点击