Android状态栏

Android状态栏(Status Bar)颜色

  • Android在4.4版本推出一个透明状态栏的概念,使手机顶部的状态栏的颜色全透明。
  • 在5.0版本推出了Material Design,可以修改状态栏颜色。
  • 所以4.4之前的版本是无法设置状态栏颜色的。

在style文件中设置状态栏的颜色

<style name="Theme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@color/color_theme</item>
<!--设置状态栏颜色-->
<item name="colorPrimaryDark">@color/color_fed952</item>
</style>

通过这张图片我们可以清楚地了解到各颜色命名值代表的意义


res/values/styles 文件中定义基础主题样式:

<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar"/>
<style name="AppTheme" parent="BaseTheme">
</style>

res/values-v19/styles 文件中定义兼容主题样式:

<style name="AppTheme" parent="BaseTheme">
<item name="android:windowTranslucentStatus">true</item>//将状态栏设置为透明
</style>

不添加代码默认显示效果为
效果如下

image

信号栏的颜色与布局一致


image

image

当我的style是这样时

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

我们在代码中添加如下的代码

 //设置状态栏的颜色透明
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

效果如下


image

小结

xml中设置状态栏透明通过

<item name="android:windowTranslucentStatus">true</item>

在代码中表示为 (注意是4.4.+以上才能实现,最好添加上版本判断)

 //设置状态栏的颜色透明
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

预留

当我们在style中添加下面的内容,系统会为我们预留状态栏的位置,状态栏的颜色和我们的布局颜色一致的

       <item name="android:fitsSystemWindows">true</item>

添加在总体style中

<!-- Base application theme. -->
    <style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:fitsSystemWindows">true</item>
    </style>

    <style name="AppTheme" parent="BaseTheme"/>

改变状态栏颜色

Android 5.0.+,API 21起,我们可以改变状态栏的颜色

1.清除透明栏
2.添加Flag来设置状态栏
3.设置状态栏颜色

//在Android 5.0.1以上使用,改变状态栏颜色

        Window window = getWindow();
        //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //设置状态栏颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.setStatusBarColor(Color.YELLOW);
        }

在我的Android 7.0上的显示效果(颜色冲击感很强,哈哈)


image

Android 6.0字体自动变色

系统默认状态栏的字体为白色,6.0的API新增了一个属性来解决这一问题。即,如果我们设置的状态栏颜色是接近于白色的话,可以在主题中添加以下属性:

<item name="android:windowLightStatusBar">true</item>
image

在V23中添加下面的内容

<style name="AppTheme" parent="BaseTheme">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowLightStatusBar">true</item>
    </style>
image

在我的魅族中不需要设置,字体就会自动变色,据说小米也行

代码中设置方式为

 Window window = getWindow();
 View decor = window.getDecorView();
        int ui = decor.getSystemUiVisibility();
        if (lightBar) {//状态栏为白色
            ui |=View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
        } else {
            ui &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
        }
        decor.setSystemUiVisibility(ui);

或者直接设置不管什么颜色都设置为黑色字体,只针对 6.0+

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//23
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

设置状态栏效果

当我们设置为

<item name="android:windowTranslucentStatus">true</item>

我们的布局会顶到状态栏


image

当我们添加了fitSystemWindows留出状态栏的位置,可是状态栏的颜色显示的是背景颜色,并不是我们想要的

 <item name="android:fitsSystemWindows">true</item>
image

我们可以采取下面的方式
1.设置统一的状态栏颜色
2.在状态栏透明的情况下,添加与状态栏高度相同的布局(比较灵活,颜色可多变)

设置统一的状态栏颜色

 <style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimaryDark">@color/colorAccent</item>
    </style>

这种方式最简单,我们只需要两步
1.设置为NoActionBar的主题
2.设置状态栏颜色


image

(ps:在我的4.4.4的手机上,没有效果)

添加状态栏高度的布局(推荐)

当我们设置了信号栏透明,即沉浸式时
第一步:获取状态栏的高度,创建一个布局他的高度就是状态栏的高度
第二步:在我们每次创建布局的时候都添加上这个布局并设置需要的颜色


import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;

/**
 * 沉浸式、版本兼容的Toolbar,状态栏透明.
 */
public class CompatToolbar extends LinearLayout {

    public CompatToolbar(Context context) {
        this(context, null);
    }

    public CompatToolbar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CompatToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setup();
    }

    public void setup() {
        int compatPadingTop = 0;
        // android 4.4
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            compatPadingTop = getStatusBarHeight();
        }
        this.setPadding(getPaddingLeft(), getPaddingTop() + compatPadingTop, getPaddingRight(), getPaddingBottom());
    }
    
    public int getStatusBarHeight() {
        int statusBarHeight = 0;
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
        Log.d("CompatToolbar", "状态栏高度:" + px2dp(statusBarHeight) + "dp");
        return statusBarHeight;
    }

//将px转为dp
    public float px2dp(float pxVal) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (pxVal / scale);
    }
}

注意我这里使用的LinearLayout布局,让该布局位于内容的上方
使用方式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.com.refreshlayout.MainActivity">
    <com.example.com.refreshlayout.CompatToolbar
        android:background="@android:color/holo_green_dark"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:text="@string/app_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

image

这种方式用起来挺不方便的,有没有更好的方式呢?
如果状态栏是白色的,系统默认字体颜色时白色的,要如何更改字体颜色呢?本篇有点长,从下一篇介绍吧。

参考文章:https://www.jianshu.com/p/a44c119d6ef7

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270