沉浸式状态栏的实现

一个开源项目实现沉浸式状态栏 简单高效

GitHub地址: https://github.com/jgilfelt/SystemBarTint
这个star还是很多的

%V59S4[%(G{XOS6%S$OS]@N.png

使用步骤

    1. 添加依赖
//状态栏
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
(1LRHY3NI[J4LT7]69{9DZR.png
    1. 在布局文件中添加属性
android:fitsSystemWindows="true"
android:clipToPadding="false"

android:fitsSystemWindows="true"
作用就是你的contentview是否忽略actionbar,title,屏幕的底部虚拟按键,将整个屏幕当作可用的空间。
正常情况,contentview可用的空间是去除了actionbar,title,底部按键的空间后剩余的可用区域;这个属性设置为true,则忽略,false则不忽略

android:clipToPadding="false"
clipToPadding:控件的绘制区域是否在padding里面, 值为true时padding那么绘制的区域就不包括padding区域;

    1. 核心代码就是个工具类
private void initSystemBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        //修改window的综合属性flags
        //WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS含义为状态栏透明
        winParams.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        win.setAttributes(winParams);
    }
    //调用开源库SystemBarTintManager进行状态栏着色 产生沉浸式效果
    SystemBarTintManager tintManager = new SystemBarTintManager(this);
    tintManager.setStatusBarTintEnabled(true);//使用状态栏着色可用
    tintManager.setStatusBarTintColor(Color.GREEN);//指定颜色进行着色
}


快捷使用

本人在项目中为了减小依赖的项目 只是把SystemBarTintManager这个管理类复制到项目中即可正常使用 无需再去进行依赖

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容