基于SystemBarTint实现
第一步
首先将SystemBarTintManager类放入项目。
下载链接:http://download.csdn.net/detail/u013193363/9339781
第二步
在activity对应的布局文件中加入两行代码
android:fitsSystemWindows=”true”
android:clipToPadding=”true”
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:clipToPadding="true"
>
第三步
onCreat()方法中调用initSystemBar()方法
private void initSystemBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
//使用颜色资源
//tintManager.setStatusBarTintResource(R.color.systemBar_color);
//使用图片资源
tintManager.setStatusBarTintDrawable(getResources().getDrawable(R.drawable.ic_top_title_background));
}
@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);
}
颜色资源文件
在res->values 新建color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<color name="systemBar_color">#2aa45a</color><!--导航栏背景颜色 -->
</resources>
此上转载于:http://blog.csdn.net/s1e1s/article/details/46558681
**至此,就可以实现,沉浸式状态栏了。
在做项目过程中,发现除了沉浸状态栏,还可以透明虚拟键,也就是页面布局会在虚拟键之下**
又看到了这个关于沉浸式的介绍也不错,可以参考看看。
http://www.jianshu.com/p/f8374d6267ef
在 KITKAT 之后,Android Window支持了一些新的属性,其中有两个是这样的 .
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS //状态栏
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION //导航栏
正如它们的变量名的意思,使用这两个属性,可以使得状态栏和导航栏变为透明,
导航栏指的就是Android下方的三大按键,当然只使用第一个属性也可以达到今天所要完成的效果。