Android 设置状态栏颜色

平常我们使用的APP,状态栏的颜色和应用里面颜色保持一致,下面介绍一下如何设置状态栏的颜色。
首先会把应用自带的标题栏隐藏,设置自己的标题栏,android:theme设置为Theme.AppCompat.NoActionBar

 <application
        android:name=".application.ContentApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
    <activity android:name=".activity.SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

现在状态栏默认是黑色的(Android7.0)。

可以通过下面一行代码设置状态栏的颜色,不过这个方法是在Android6.0之后才有的。

activity.getWindow().setStatusBarColor(statusColor);

如果低于Android6.0需要其他的设置方法,下面写了一个工具类来实现

package com.wzj.safe.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import com.wzj.safe.R;

/**
 * Created by wzj on 2017/12/1.
 */
public class StatusBarUtil
{
    private static final int INVALID_VAL = -1;
    private static final int COLOR_DEFAULT = Color.parseColor("#20000000");

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static void compat(Activity activity, int statusColor)
    {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            if (statusColor != INVALID_VAL)
            {
                activity.getWindow().setStatusBarColor(statusColor);
            }
            return;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        {
            int color = COLOR_DEFAULT;
            ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
            if (statusColor != INVALID_VAL)
            {
                color = statusColor;
            }
            View statusBarView = new View(activity);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    getStatusBarHeight(activity));
            statusBarView.setBackgroundColor(color);
            contentView.addView(statusBarView, lp);
        }

    }

    public static void compat(Activity activity)
    {
        compat(activity, INVALID_VAL);
    }


    public static int getStatusBarHeight(Context context)
    {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0)
        {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}

在Activity的onCreate()方法加上如下代码StatusBarUtil.compat(this,getColor(R.color.systemBackground))就可以了

@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    StatusBarUtil.compat(this,getColor(R.color.systemBackground));
}

设置之后的样式如下

###参考资料
http://blog.csdn.net/lmj623565791/article/details/48649563