(三)Android沉浸式状态栏透明度不同机型适配解决方案(尤其系统4.4及以下出现的适配问题)

上图为系统4.4及以下沉浸式出现的适配问题图片

1. 沉浸式实现原理就是使整个activity布局延伸到整个屏幕,然后使状态栏变成透明色,有些手机会有导航栏,同样也可以把导航栏变成透明色,这样会使一些app更加美观。

2.废话不多说了,直接上代码,步骤如下:

1、引入

1.1 github仓库地址https://github.com/gyf-dev/ImmersionBar

1.2 gradle文件中加入

dependencies {compile'com.gyf.barlibrary:barlibrary:2.3.0'}

2 特性

1)基本介绍

基础用法,建议在BaseActivity里调用

```

public class BaseActivity extends AppCompatActivity {

@Override

  protected void onCreate(@Nullable Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

        ImmersionBar.with(this).init(); //初始化,默认透明状态栏和黑色导航栏

    }

    @Override

    protected void onDestroy() {

        super.onDestroy();

        ImmersionBar.with(this).destroy(); //不调用该方法,如果界面bar发生改变,在不关闭app的情况下,退出此界面再进入将记忆最后一次bar改变的状态

    }

}

```

到此已解决上图所显示的适配问题,就这么简单,两三行代码搞定。

3. 高级用法,如果基础用法不能满足你的需求,可以试试这里的方法

```

ImmersionBar.with(this).transparentStatusBar()  //透明状态栏,不写默认透明色

.transparentNavigationBar() //透明导航栏,不写默认黑色(设置此方法,fullScreen()方法自动为true)    

.transparentBar()       //透明状态栏和导航栏,不写默认状态栏为透明色,导航栏为黑色(设置此方法,fullScreen()方法自动为true)

.statusBarColor(R.color.colorPrimary)     //状态栏颜色,不写默认透明色

.navigationBarColor(R.color.colorPrimary) //导航栏颜色,不写默认黑色

.barColor(R.color.colorPrimary) //同时自定义状态栏和导航栏颜色,不写默认状态栏为透明色,导航栏为黑色

.statusBarAlpha(0.3f) //状态栏透明度,不写默认0.0f           

.navigationBarAlpha(0.4f) //导航栏透明度,不写默认0.0F               

.barAlpha(0.3f) //状态栏和导航栏透明度,不写默认0.0f                

.statusBarDarkFont(true)  //状态栏字体是深色,不写默认为亮色       

.flymeOSStatusBarFontColor(R.color.btn3) //修改flyme OS状态栏字体颜色                

.fullScreen(true)     //有导航栏的情况下,activity全屏显示,也就是activity最下面被导航栏覆盖,不写默认非全屏               

.hideBar(BarHide.FLAG_HIDE_BAR)  //隐藏状态栏或导航栏或两者,不写默认不隐藏               

.addViewSupportTransformColor(toolbar) //设置支持view变色,可以添加多个view,不指定颜色,默认和状态栏同色,还有两个重载方法

.titleBar(view)    //解决状态栏和布局重叠问题,任选其一         

.statusBarView(view)  //解决状态栏和布局重叠问题,任选其一 

.fitsSystemWindows(true)   //解决状态栏和布局重叠问题,任选其一,默认为false,当为true时一定要指定statusBarColor(),不然状态栏为透明色

.supportActionBar(true) //支持ActionBar使用           

.statusBarColorTransform(R.color.orange)  //状态栏变色后的颜色                

.navigationBarColorTransform(R.color.orange) //导航栏变色后的颜色

.barColorTransform(R.color.orange)  //状态栏和导航栏变色后的颜色                

.removeSupportView(toolbar)  //移除指定view支持                

.removeSupportAllView() //移除全部view支持

.addTag("tag")  //给以上设置的参数打标记

.getTag("tag")  //根据tag获得沉浸式参数                       

.reset()  //重置所以沉浸式参数                         

.keyboardEnable(true) //解决软键盘与底部输入框冲突问题,默认为false                           

.setOnKeyboardListener(new OnKeyboardListener() {    //软键盘监听回调         

@Override

    public void onKeyboardChange(boolean isPopup, int keyboardHeight) {

                       LogUtils.e(isPopup);  //isPopup为true,软键盘弹出,为false,软键盘关闭

                   }

              })



.keyboardMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) //单独指定软键盘模式

.init();//必须调用方可沉浸式

```


补充:

4.4以下真正实现沉浸式可以使用统一的标题栏布局效果图和代码类似如下:


include_title.xml:

、、、

?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/title_bar"

    android:layout_width="match_parent"

    android:layout_height="64dp"

    android:background="?attr/colorPrimary"

    android:gravity="bottom"

    android:orientation="horizontal">

        android:id="@+id/ll_left"

        android:layout_width="0dp"

        android:layout_height="40dp"

        android:layout_weight="1.7"

        android:gravity="start|center_vertical"

        android:orientation="horizontal">








            android:id="@+id/tv_title_left"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:layout_marginLeft="4dp"

            android:gravity="center_vertical"

            android:maxLines="1"

            android:textColor="@color/white"

            android:textSize="14sp" />

        android:id="@+id/tv_title"

        android:layout_width="0dp"

        android:layout_height="40dp"

        android:layout_weight="3"

        android:ellipsize="end"

        android:gravity="center"

        android:maxLines="1"

        android:textColor="@color/white"

        android:textSize="18sp" />

        android:layout_width="0dp"

        android:layout_height="40dp"

        android:layout_weight="1.7"

        android:gravity="end|center_vertical">

            android:id="@+id/tv_title_right_copy"

            android:layout_width="wrap_content"

            android:layout_height="match_parent"

            android:text="复制手机号"

            android:drawablePadding="2dp"

            android:padding="5dp"

            android:gravity="center_vertical"

            android:textColor="@color/white"

            android:textSize="14sp"

            android:visibility="gone"/>

</LinearLayout>

、、、

在宿主Acitivity中进行引用

<include layout="@layout/include_title">

同时切记Activity中的布局中最外层不需要 添加android:fitsSystemWindows=”true”,不然不能实现沉浸式的效果。

加上android:fitsSystemWindows=”true”这句话之后布局文件就会自动在状态栏下开始布局,而不加这句并且在4.4以后加上

透明状态栏之后你的activity的布局就会从整个手机屏幕顶端开始布局,而不是从状态栏之后。所以,加上透明状态栏之后说明你的布局被上移了,然后就

需要手动来测量状态栏的高度,并且给toolBar(我这里的布局,你的布局不确定哦)设置margin了。

详细介绍请参考博文:https://blog.csdn.net/ling9400/article/details/59478358

4、总结

      至此,ImmersionBar库的基本用法与原理讲完了。网上关于沉浸式的介绍铺天盖地,但是很少有人把它们封装起来,当开发者调用的时候还得自己去写大量代码,消耗大家时间。正好有时间亲测管用,也整理了一下方便呀以后的开发,解决大家在沉浸式方面出现的问题。如果还有不懂得地方可以去demo里看看,或者直接底下留言!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容