一 简介
最近做了个项目,公司让做成沉浸式状态栏的效果,这个是安卓4.4以上版本有的特性。自己从网上搜集了很多资料,发现网上的方法都比较麻烦,自己就写了个简单的方法,和大家分享。
效果图如下:
关于这个沉浸式状态栏也叫Translucent Bars。
二 实现方法
1 首先,我们需要引用一个jar包,比库简洁。
compile files('libs/systembartint-1.0.4.jar');
2 我们要加一个属性,这个属性是让手机适配整个屏幕,实现沉浸式状态。有两种方式实现,我们可以在布局文件中加上:fitsSystemWindows="true",也可以在Style中加上这句话。
1)
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:fitsSystemWindows">true</item>
</style>
2)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#FFDD00">
3 在代码中在activity中的oncreate()中,设置一段代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintColor(getResources().getColor(R.color.title_yellow));
tintManager.setStatusBarTintEnabled(true);
}
如果像引导页不需要沉浸式,则只需:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
这样之后,运行项目就可以实现沉浸式状态栏的效果,颜色都是可以在values中设置。没有像Toolbar一样在布局文件那么繁琐,就可以实现一样的效果。又是美好的建议请大神提出来,我是个安卓小白,一起加油。