参考了一些文章做了一些修改,变成了自己的工具类。其中有很多地方欠考虑,有待改进,欢迎路过的大佬给点建议。
经过前两篇的介绍我们对如何修改状态栏的效果有了大致的了解,本篇介绍一种使用更加简单的方式
设置主题效果
注意: 要设置fitsSystemWindows为true
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:fitsSystemWindows">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
参考SystemBarTint对状态栏的改变方式,添加下面这个类方法,用来设置 API 19 以上的状态栏效果
我们的界面绘制时在DecorView中的,这个类的核心思想就是:
1.获取DecorView
2.绘制状态栏高度的View
3.将view添加到DecorView中
所以我们对状态栏的操作,其实就是对view的操作。在这个类中默认view不显示,改变状态栏的效果时,要调用
setStatusBarTintEnabled(true)
注意:使用这个方法的前提是 状态栏是透明的,因为进行了相关的判断(可以自行修改)
public class SystemBarTintManager {
public static final int DEFAULT_TINT_COLOR = 0x99000000;
private boolean mStatusBarAvailable;
private boolean mStatusBarTintEnabled;
private View mStatusBarTintView;
@TargetApi(19)
public SystemBarTintManager(Activity activity) {
Window win = activity.getWindow();
//获取DecorView
ViewGroup decorViewGroup = (ViewGroup) win.getDecorView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// 检查主题中是否有透明的状态栏
int[] attrs = {android.R.attr.windowTranslucentStatus};
TypedArray a = activity.obtainStyledAttributes(attrs);
try {
mStatusBarAvailable = a.getBoolean(0, false);
} finally {
a.recycle();
}
WindowManager.LayoutParams winParams = win.getAttributes();
int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;//状态栏透明
if ((winParams.flags & bits) != 0) {
mStatusBarAvailable = true;
}
}
if (mStatusBarAvailable) {
setupStatusBarView(activity, decorViewGroup);
}
}
/**
* 初始化状态栏
*
* @param context
* @param decorViewGroup
*/
private void setupStatusBarView(Activity context, ViewGroup decorViewGroup) {
mStatusBarTintView = new View(context);
//设置高度为Statusbar的高度
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, getStatusBarHeight(context));
params.gravity = Gravity.TOP;
mStatusBarTintView.setLayoutParams(params);
mStatusBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR);
//默认不显示
mStatusBarTintView.setVisibility(View.GONE);
//decorView添加状态栏高度的View
decorViewGroup.addView(mStatusBarTintView);
}
/**
* 获取状态栏高度
*
* @param activity
* @return
*/
private int getStatusBarHeight(Activity activity) {
int statusBarHeight = 0;
int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
/**
* 显示状态栏
*/
public void setStatusBarTintEnabled(boolean enabled) {
mStatusBarTintEnabled = enabled;
if (mStatusBarAvailable) {
mStatusBarTintView.setVisibility(enabled ? View.VISIBLE : View.GONE);
}
}
/**
* 设置状态栏颜色
*
* @param color
*/
public void setStatusBarTintColor(int color) {
if (mStatusBarAvailable) {
mStatusBarTintView.setBackgroundColor(color);
}
}
}
工具类代码
在这段代码中当我们的API 为4.4.+ 但是小于 5.0时使用上述的类
/**
* 修改状态栏颜色,支持4.4以上版本
* @param colorId 颜色
*/
public static void setStatusBarColor(Activity activity, int colorId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.setStatusBarColor(colorId);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//使用SystemBarTintManager,需要先将状态栏设置为透明
setTranslucentStatus(activity);
SystemBarTintManager systemBarTintManager = new SystemBarTintManager(activity);
systemBarTintManager.setStatusBarTintEnabled(true);//显示状态栏
systemBarTintManager.setStatusBarTintColor(colorId);//设置状态栏颜色
}
}
完整代码
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.IntDef;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Created by rhm on 2018/1/13.
*/
public class StatusBarUtil {
public final static int TYPE_MIUI = 0;
public final static int TYPE_FLYME = 1;
public final static int TYPE_M = 3;//6.0
@IntDef({TYPE_MIUI,
TYPE_FLYME,
TYPE_M})
@Retention(RetentionPolicy.SOURCE)
@interface ViewType {
}
/**
* 修改状态栏颜色,支持4.4以上版本
* @param colorId 颜色
*/
public static void setStatusBarColor(Activity activity, int colorId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.setStatusBarColor(colorId);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//使用SystemBarTintManager,需要先将状态栏设置为透明
setTranslucentStatus(activity);
SystemBarTintManager systemBarTintManager = new SystemBarTintManager(activity);
systemBarTintManager.setStatusBarTintEnabled(true);//显示状态栏
systemBarTintManager.setStatusBarTintColor(colorId);//设置状态栏颜色
}
}
/**
* 设置状态栏透明
*/
@TargetApi(19)
public static void setTranslucentStatus(Activity activity) {
// 5.0以上系统状态栏透明
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
//清除透明状态栏
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//设置状态栏颜色必须添加
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);//设置透明
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //19
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
/**
* 设置沉浸式状态栏
*
* @param fontIconDark 状态栏字体和图标颜色是否为深色
*/
public static void setImmersiveStatusBar(Activity activity,boolean fontIconDark) {
setTranslucentStatus(activity);
if (fontIconDark) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
setStatusBarFontIconDark(activity,TYPE_M);
} else if (OSUtils.isMiui()) {
setStatusBarFontIconDark(activity,TYPE_MIUI);
} else if (OSUtils.isFlyme()) {
setStatusBarFontIconDark(activity,TYPE_FLYME);
} else {//其他情况下我们将状态栏设置为灰色,就不会看不见字体
setStatusBarColor(activity,Color.LTGRAY);//灰色
}
}
}
/**
* 设置文字颜色
*/
public static void setStatusBarFontIconDark(Activity activity,@ViewType int type) {
switch (type) {
case TYPE_MIUI:
setMiuiUI(activity,true);
break;
case TYPE_M:
setCommonUI(activity);
break;
case TYPE_FLYME:
setFlymeUI(activity,true);
break;
}
}
//设置6.0的字体
public static void setCommonUI(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
activity.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
//设置Flyme的字体
public static void setFlymeUI(Activity activity,boolean dark) {
try {
Window window = activity.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
darkFlag.setAccessible(true);
meizuFlags.setAccessible(true);
int bit = darkFlag.getInt(null);
int value = meizuFlags.getInt(lp);
if (dark) {
value |= bit;
} else {
value &= ~bit;
}
meizuFlags.setInt(lp, value);
window.setAttributes(lp);
} catch (Exception e) {
e.printStackTrace();
}
}
//设置MIUI字体
public static void setMiuiUI(Activity activity,boolean dark) {
try {
Window window = activity.getWindow();
Class clazz = activity.getWindow().getClass();
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
int darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
if (dark) { //状态栏亮色且黑色字体
extraFlagField.invoke(window, darkModeFlag, darkModeFlag);
} else {
extraFlagField.invoke(window, 0, darkModeFlag);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用方式
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StatusBarUtil.setImmersiveStatusBar(this,true);
}
}
效果
请在github上下载完整的内容,工具类在statusbarutil中。