Android实现ImmersionBar沉浸式状态栏

(一)效果图

(二)实现步骤:

1、在build.gradle中加上

implementation 'com.gyf.barlibrary:barlibrary:2.3.0'

2、设置页面为全屏

将上图中的

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

改为

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

3、 加入以下代码段

ImmersionBar mImmersionBar = ImmersionBar.with(this)
                .statusBarColor(R.color.colorbar)//设置状态栏背景颜色
                .statusBarDarkFont(false);//设置状态栏字体,true为暗色,不写或者false为亮色
mImmersionBar.init();
@Override
protected void onDestroy() {
    super.onDestroy();
    // 必须调用该方法,防止内存泄漏
    ImmersionBar.with(this).destroy();
}

4、完成上面三步,会发现页面布局会上移,解决方法就是在布局文件中加入:

android:clipToPadding="true"
android:fitsSystemWindows="true"

猜你喜欢

转载自blog.csdn.net/weixin_46211609/article/details/129733069