/StatusView

Customize StatusBar for Android platform.

Primary LanguageJavaApache License 2.0Apache-2.0

状态栏一体化,包括随着滑动渐变,透明状态栏,黑色字体的状态栏,兼容国产魅族、小米手机,其它手机使用标准模式。

我的主页:http://www.yanzhenjie.com
欢迎关注我的微博:http://weibo.com/yanzhenjieit

QQ技术交流群:547839514

截图

如何使用

依赖本项目后,使用 StatusViewStatusUtils简单几行代码即可完成状态栏一体化。

依赖

  • Gradle
compile 'com.yanzhenjie:statusview:1.0.0'
  • Maven
<dependency>
  <groupId>com.yanzhenjie</groupId>
  <artifactId>statusview</artifactId>
  <version>1.0.0</version>
  <type>pom</type>
</dependency>

类和方法介绍

  1. StatusView:用来替代系统的Status,开发者只需要控制它的background属性即可,其它都会自动处。
  2. StatusUtils:用来设置Activity的状态栏和导航栏,这个类的方法要着重说明。
  • setStatusBarColor:设置状态栏颜色。
  • setNavigationBarColor:设置导航栏颜色。
  • setSystemBarColor:同时设置状态栏、导航栏颜色,每个颜色可以单独指定。
  • setLayoutFullScreen:设置ActivityContentView占满全屏,并让StatusBar透明,内容侵入状态栏下层。
  • setStatusBarDarkFont:设置状态栏的字体为深色,一般用于当Toolbar和状态栏为浅色时(比如白色状态栏)。

图一效果实现

ContentView的顶部的第一个ViewStatusView,然后调用setLayoutFullScreenStatusView顶到状态栏下层,接下来就可以控制状态栏颜色了。

布局如下:

<LinearLayout android:id="@+id/root_layout">

    <LinearLayout>
        <com.yanzhenjie.statusview.StatusView
            <!-- 宽高随便指定 -->
            android:background="?attr/colorPrimary"
            app:fitsView="@id/root_layout"/>

        <android.support.v7.widget.Toolbar/>
    </LinearLayout>

    <.../>
    
</LinearLayout>

布局伪代码如上所示,app:fitsView="@id/root_layout"这个属性必须要指定,并且只能通过xml布局指定,它的作用是适配Android5.0以下时避免霸占了状态栏,但是不能修改状态栏颜色的问题。

接着在Activity#onCreate()中调用setLayoutFullScreen

setContentView(...);
StatusUtils.setLayoutFullScreen(this); // Layout full screen.

接下来随意修改状态栏颜色:

mStatusView.setBackgroundColor(Color.BLUE);

图二效果实现

使用FrameLayout作为root,底层放需要侵入状态栏的View,上层放StatusView,然后调用setLayoutFullScreenStatusView顶到状态栏下层,接下来就可以控制状态栏的透明度了。

布局如下:

<FrameLayout android:id="@+id/root_layout">

    <.../>

    <LinearLayout>

        <com.yanzhenjie.statusview.StatusView
            <!-- 宽高随便指定 -->
            android:background="?attr/colorPrimary"
            app:fitsView="@id/root_layout"/>

        <android.support.v7.widget.Toolbar/>

    </LinearLayout>

</FrameLayout>

接着在Activity#onCreate()中调用setLayoutFullScreen

setContentView(...);
StatusUtils.setLayoutFullScreen(this); // Layout full screen.

接下来随意修改状态栏的透明度:

mStatusView.getBackground().mutate().setAlpha(0~255); // 透明度值是[0, 255]。

图三效果实现

图三就比较简单了,因为它是修改状态栏的字体为深色:

StatusUtils.setStatusBarDarkFont(this, true); // Dark font for StatusBar.

因为Android在6.0以前,状态栏字体都是白色。在6.0以后可以修改状态栏字体为深色字体了,但是还是不能指定具体改为什么颜色,所以建议在6.0以前不要使用白色状态栏,开发者可以在BaseActivity中加入如下代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    StatusUtils.setStatusBarColor(this, Color.WHITE);
    StatusUtils.setStatusBarDarkFont(this, true); // Dark font for StatusBar.
} else {
    StatusUtils.setStatusBarColor(this, Color.GRAY);
}

License

Copyright 2017 Yan Zhenjie

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.