Android 状态栏工具类(实现沉浸式状态栏/变色状态栏)

来源:互联网 发布:音序器软件 编辑:程序博客网 时间:2024/06/05 11:28

From:写代码的猴子

这是一个为Android App 设置状态栏的工具类, 可以在4.4及其以上系统中实现 沉浸式状态栏/状态栏变色,支持设置状态栏透明度,满足你司设计师的各种要求(雾)。

在此之前我写过一篇Android App 沉浸式状态栏解决方案,后来我司设计师说默认的透明度太深了,让我改浅一点,然后在想了一些办法之后给解决了。本着不重复造轮子的原则,索性整理成一个工具类,方便需要的开发者。

项目 GitHub 地址

Sample 下载

下载 StatusBarUtil-Demo

特性

1. 设置状态栏颜色
StatusBarUtil.setColor(Activity activity, int color)

2. 设置状态栏半透明
StatusBarUtil.setTranslucent(Activity activity, int statusBarAlpha)

3. 设置状态栏全透明
StatusBarUtil.setTransparent(Activity activity)

4. 为包含 DrawerLayout 的界面设置状态栏颜色(也可以设置半透明和全透明)
StatusBarUtil.setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, int color)

5. 通过传入 statusBarAlpha 参数,可以改变状态栏的透明度值,默认值是112。

使用

1. 在 build.gradle 文件中添加依赖, StatusBarUtil 已经发布在 JCenter:
compile 'com.jaeger.statusbaruitl:library:1.0.0'
2. 在 setContentView() 之后调用你需要的方法,例如:
setContentView(R.layout.main_activity);...StatusBarUtil.setColor(MainActivity.this, mColor);
3. 如果你在一个包含 DrawerLayout 的界面中使用, 你需要在布局文件中为 DrawerLayout 添加 android:fitsSystemWindows="true" 属性:
<android.support.v4.widget.DrawerLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/drawer_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true">    ...</android.support.v4.widget.DrawerLayout>
4. 当你设置了 statusBarAlpha 值时,该值需要在 0 ~ 255 之间

最后

如果你有任何建议或问题,请及时联系我。如果你对这个工具类有优化,欢迎 fork 提 pr。

传送门 GitHub 地址


0 0