看了很多人写的状态栏一体化的文章,过程过于复杂详细了,之前有看过一篇文章讲的很棒,可惜一时类似题目太多竟然找不到了,于是为了自己也为了他人方便,写一遍自己已经实现并且多次用到项目中的状态栏一体化方法,其中相信大部分都引入的是Github上的Demo项目https://github.com/niorgai/StatusBarCompat
首先是下载项目地址中的library工程,然后关联到项目中,并且添加依赖,AS项目添加依赖,从File-New-New Module中添加项目,作为一个mudule,添加后右键项目添加Add library依赖。
在eclipse中import 导入library项目,然后点击原项目build依赖项目
这时候就可以直接调用相关的类了,在你的项目BaseActivity基类中添加他的方法,这样每个集成了BaseActivity的界面都可以实现:
// 设置状态栏在oncreate方法中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置状态栏
setTranslucentStatus(R.color.main_color);
}
public void setTranslucentStatus(int colorTitler) {
// 判断是否是4.4及以上版本、
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
winParams.flags |= bits;
win.setAttributes(winParams);
}
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setNavigationBarTintEnabled(true);
tintManager.setNavigationBarTintResource(colorTitler);
tintManager.setStatusBarTintResource(colorTitler);
}
这里需要说明的是,可以改变状态栏的是基于安卓4.4以上的版本,所以要做权限版本判断,另外这里之所以写成这样,是方便界面修改状态栏,虽然大部分界面保持一个主题色,但不代表有个别界面需要修改状态栏颜色,所以写成该方法
另外需要注意的是,在资源文件中,要标明属性值 android:fitsSystemWindows="true"
所有Activity继承BaseActivity,很简单实现状态栏一体化,不需要写的那么复杂
Eclipse需要的library项目下载http://download.csdn.net/detail/wangyetongsss/9783367(Github分离出来的)