`

状态栏颜色

 
阅读更多

 

导语:沉浸式状态栏,改变状态栏的颜色使之与APP风格一体化是不是感觉很漂亮,很美?其实实现这种效果并不难,google在4.4及以下提供了相关的方法。

 

我相信大家肯定看到过很多软件有沉浸式状态栏,在运行该App时改变了手机屏幕顶部状态栏的颜色,使他们的风格非常的统一,看起来异常的漂亮和清爽。想不想实现这种效果呢,其实在Android KITKAT上有一个新的特性可以设置手机状态栏的背景,让手机整个界面的风格保持一致,看起来非常清爽统一。当然这种效果只支持在API 19及以上使用沉浸式状态。4.4系统以上的是看不到这种效果的。

 

效果图

 

方法实现

 

1添加布局属性

compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'

 

 

首先要在布局文件中加入下面两个属性:

android:clipToPadding="true"

android:fitsSystemWindows="true"

 

解释一下上面两个布局属性的意思:

android:clipToPadding 定义布局间是否有间距

android:fitsSystemWindows="true" 意思就是设置应用布局时是否考虑系统窗口布局;如果为true,将调整系统窗口布局以适应你自定义的布局。比如系统有状态栏,应用也有状态栏时。看你这个布局代码,恰恰是在定义标题栏样式,所以用到这行代码了。

 

 

2在Activity中应用一下方法

 

public static void initSystemBar(Activity activity) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

setTranslucentStatus(activity, true);

}

SystemBarTintManager tintManager = new SystemBarTintManager(activity);

tintManager.setStatusBarTintEnabled(true);

// 使用颜色资源

tintManager.setStatusBarTintResource(R.color.status_color);

}

 

 

@TargetApi(19)

private static void setTranslucentStatus(Activity activity, boolean on) {

Window win = activity.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);

}

 

 

3SystemBarTintManager 的使用

 

SystemBarTintManager 是状态栏的管理实例,没有它是不行的,它的开源地址是:https://github.com/hexiaochun/SystemBarTint ,已经封装的非常好了,我们只需要把它下载下来,应用到你的App中即可。

 

 

到这里就介绍完了,是不是很简单,赶紧去试试吧。

 

注意

想要源码的,关注公众号,发送关键字“源码”即可获得。

 

 

 

 

 

 

 

Android改变状态栏statusbar背景色

Android开发中在某些界面为了保证显示一致性,可能需要调整statusBar的背景色,本文介绍了Android 4.4(API 19)和Android 5.0以上修改statusBar背景色的方案。其中5.0只需要修改styles.xml文件就可以修改statusbar背景色,而4.4使用了Toolbar来替代ActionBar的方案。

support-v7-appcompat在21版本之后做了很多修改,所以首先保证你的v7包在21以上。如果没有,请在Android Manager把v7包升级到该版本,或者在Android Studio gradle中如下

dependencies {
    compile 'com.android.support:appcompat-v7:21.1.+'
}

1、修改styles.xml文件

<!-- 这个是给5.0使用的Style,其实完全可以在源代码中加判断这样就不需要多个style文件-->
<style name="AppTheme" parent="Theme.AppCompat">
        <item name="android:textColorPrimary">#000000</item>
        <item name="colorPrimary">#ff0000</item>
        <item name="colorPrimaryDark">#00ff00</item>
        <item name="colorAccent">#0000ff</item>
        <!-- Customize your theme here. -->
    </style>
    <!-- 这个是给4.4使用的Style,其实完全可以在源代码中加判断这样就不需要多个style文件-->
    <style name="AppThemeNoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:textColorPrimary">#000000</item>
        <item name="colorPrimary">#ff0000</item>
        <item name="colorPrimaryDark">#00ff00</item>
        <item name="colorAccent">#0000ff</item>
        <item name="android:windowTranslucentStatus" tools:targetApi="19">true</item> #这句话是关键,状态栏透明
        <!-- Customize your theme here. -->
    </style>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics