由于市面上手机越来越多,状态栏五花八门,导致我们开发越来越混乱,这里总结一下状态栏开发需要注意什么。
Android 4.4 之前,Android 的状态栏是黑色背景,无法修改。 (不需要适配)
Android 4.4 推出了透明状态栏的效果。 (不需要适配)
Android 5.0 提供了方法可以直接修改状态栏的颜色。
一下方法只针对Android 5.0+,5.0一下不做适配,因为不需要。
阅读此篇只需要花费5分钟时间
首先介绍下正常创建一个项目AppTheme
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
隐藏状态栏,ActionBar
1.隐藏状态栏和ActionBar,可自行修改是否显示ActionBar
@style/Theme.AppCompat.Light.NoActionBar
android:theme="@style/FullScreenTheme"
<style name="FullScreenTheme" parent="AppTheme">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
2.在setContentView之前执行
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
3.代码实现
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //隐藏状态栏
// getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) //显示状态栏
沉浸式状态
目前很多主流的APP都是这样实现的, StatusBarUtil.immersive(this);
可以看见这样布局出现在状态栏的底部,
在代码设置填充一个状态栏高度的view
就可以实现大多数APP填充状态栏的想法了
StatusBarUtil.setPaddingSmart(this, textView);
/**
* 增加View的paddingTop,增加的值为状态栏高度 (智能判断,并设置高度)
*/
public static void setPaddingSmart(Context context, View view) {
if (Build.VERSION.SDK_INT >= MIN_API) {
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp != null && lp.height > 0) {
lp.height += getStatusBarHeight(context);//增高
}
view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + getStatusBarHeight(context),
view.getPaddingRight(), view.getPaddingBottom());
}
}
/**
* 获取状态栏高度
*/
public static int getStatusBarHeight(Context context) {
int result = 24;
int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resId > 0) {
result = context.getResources().getDimensionPixelSize(resId);
} else {
result = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
result, Resources.getSystem().getDisplayMetrics());
}
return result;
}
状态栏白底黑字的实现,终极解决所有状态栏
再也不用担心状态栏干嘛干嘛的了,直接放弃设计,直接白底黑字,
目前微信微博都是这样实现的,个别特殊页面还是沉浸式比较好看。请看上面代码
仿微信,微博实现状态栏不操作,白色背景,黑色字体的效果
<item name="android:windowLightStatusBar">true</item>
状态栏颜色可以修改:android:statusBarColor
<style name="appTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textColor">@color/textColor</item>
<item name="android:textSize">@dimen/textSize</item>
<item name="android:editTextColor">@color/editTextColor</item>
<item name="android:textColorHint">@color/textColorHint</item>
<item name="colorPrimary">@color/textColorPrimary</item>
<item name="android:statusBarColor">@color/statusBarColor</item>
<item name="android:windowLightStatusBar">true</item>
<item name="android:windowAnimationStyle">@style/fade</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@color/windowBackground</item>
<item name="android:actionBarSize">@dimen/actionBarSize</item>
</style>
StatusBarUtil代码
public class StatusBarUtil {
public static int DEFAULT_COLOR = 0;
public static float DEFAULT_ALPHA = 0;//Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 0.2f : 0.3f;
public static final int MIN_API = 19;
//<editor-fold desc="沉侵">
public static void immersive(Activity activity) {
immersive(activity, DEFAULT_COLOR, DEFAULT_ALPHA);
}
//<editor-fold desc="沉侵">
public static void immersive(Activity activity, boolean isStatusBarColor) {
immersive(activity, DEFAULT_COLOR, DEFAULT_ALPHA, isStatusBarColor);
}
public static void immersive(Activity activity, int color, @FloatRange(from = 0.0, to = 1.0) float alpha) {
immersive(activity.getWindow(), color, alpha);
}
public static void immersive(Activity activity, int color, @FloatRange(from = 0.0, to = 1.0) float alpha, boolean isStatusBarColor) {
immersive(activity.getWindow(), color, alpha, isStatusBarColor);
}
public static void immersive(Activity activity, int color) {
immersive(activity.getWindow(), color, 1f);
}
public static void immersive(Window window) {
immersive(window, DEFAULT_COLOR, DEFAULT_ALPHA);
}
public static void immersive(Window window, int color) {
immersive(window, color, 1f);
}
public static void immersive(Window window, int color, @FloatRange(from = 0.0, to = 1.0) float alpha) {
if (Build.VERSION.SDK_INT >= 21) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(mixtureColor(color, alpha));
int systemUiVisibility = window.getDecorView().getSystemUiVisibility();
systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
window.getDecorView().setSystemUiVisibility(systemUiVisibility);
} else if (Build.VERSION.SDK_INT >= 19) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
setTranslucentView((ViewGroup) window.getDecorView(), color, alpha);
} else if (Build.VERSION.SDK_INT >= MIN_API && Build.VERSION.SDK_INT > 16) {
int systemUiVisibility = window.getDecorView().getSystemUiVisibility();
systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
window.getDecorView().setSystemUiVisibility(systemUiVisibility);
}
}
public static void immersive(Window window, int color, @FloatRange(from = 0.0, to = 1.0) float alpha, boolean isStatusBarColor) {
if (Build.VERSION.SDK_INT >= 21) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// if(isStatusBarColor){
// window.setStatusBarColor(Color.argb(255, 244, 62, 121));
// }else{
// window.setStatusBarColor(mixtureColor(color, alpha));
// }
window.setStatusBarColor(mixtureColor(color, alpha));
int systemUiVisibility = window.getDecorView().getSystemUiVisibility();
systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
window.getDecorView().setSystemUiVisibility(systemUiVisibility);
} else if (Build.VERSION.SDK_INT >= 19) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
setTranslucentView((ViewGroup) window.getDecorView(), color, alpha);
} else if (Build.VERSION.SDK_INT >= MIN_API && Build.VERSION.SDK_INT > 16) {
int systemUiVisibility = window.getDecorView().getSystemUiVisibility();
systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
window.getDecorView().setSystemUiVisibility(systemUiVisibility);
}
}
//</editor-fold>
//<editor-fold desc="DarkMode">
public static void darkMode(Activity activity, boolean dark) {
if (isFlyme4Later()) {
darkModeForFlyme4(activity.getWindow(), dark);
} else if (isMIUI6Later()) {
darkModeForMIUI6(activity.getWindow(), dark);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
darkModeForM(activity.getWindow(), dark);
}
}
/**
* 设置状态栏darkMode,字体颜色及icon变黑(目前支持MIUI6以上,Flyme4以上,Android M以上)
*/
public static void darkMode(Activity activity) {
darkMode(activity.getWindow(), DEFAULT_COLOR, DEFAULT_ALPHA);
}
public static void darkMode(Activity activity, int color, @FloatRange(from = 0.0, to = 1.0) float alpha) {
darkMode(activity.getWindow(), color, alpha);
}
/**
* 设置状态栏darkMode,字体颜色及icon变黑(目前支持MIUI6以上,Flyme4以上,Android M以上)
*/
public static void darkMode(Window window, int color, @FloatRange(from = 0.0, to = 1.0) float alpha) {
if (isFlyme4Later()) {
darkModeForFlyme4(window, true);
immersive(window, color, alpha);
} else if (isMIUI6Later()) {
darkModeForMIUI6(window, true);
immersive(window, color, alpha);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
darkModeForM(window, true);
immersive(window, color, alpha);
} else if (Build.VERSION.SDK_INT >= 19) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
setTranslucentView((ViewGroup) window.getDecorView(), color, alpha);
} else {
immersive(window, color, alpha);
}
// if (Build.VERSION.SDK_INT >= 21) {
// window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// window.setStatusBarColor(Color.TRANSPARENT);
// } else if (Build.VERSION.SDK_INT >= 19) {
// window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// }
// setTranslucentView((ViewGroup) window.getDecorView(), color, alpha);
}
//------------------------->
/**
* android 6.0设置字体颜色
*/
@RequiresApi(Build.VERSION_CODES.M)
private static void darkModeForM(Window window, boolean dark) {
// window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// window.setStatusBarColor(Color.TRANSPARENT);
int systemUiVisibility = window.getDecorView().getSystemUiVisibility();
if (dark) {
systemUiVisibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
} else {
systemUiVisibility &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
window.getDecorView().setSystemUiVisibility(systemUiVisibility);
}
/**
* 设置Flyme4+的darkMode,darkMode时候字体颜色及icon变黑
* http://open-wiki.flyme.cn/index.php?title=Flyme%E7%B3%BB%E7%BB%9FAPI
*/
public static boolean darkModeForFlyme4(Window window, boolean dark) {
boolean result = false;
if (window != null) {
try {
WindowManager.LayoutParams e = 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(e);
if (dark) {
value |= bit;
} else {
value &= ~bit;
}
meizuFlags.setInt(e, value);
window.setAttributes(e);
result = true;
} catch (Exception var8) {
Log.e("StatusBar", "darkIcon: failed");
}
}
return result;
}
/**
* 设置MIUI6+的状态栏是否为darkMode,darkMode时候字体颜色及icon变黑
* http://dev.xiaomi.com/doc/p=4769/
*/
public static boolean darkModeForMIUI6(Window window, boolean darkmode) {
Class<? extends Window> clazz = window.getClass();
try {
int darkModeFlag = 0;
Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
extraFlagField.invoke(window, darkmode ? darkModeFlag : 0, darkModeFlag);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 判断是否Flyme4以上
*/
public static boolean isFlyme4Later() {
return Build.FINGERPRINT.contains("Flyme_OS_4")
|| Build.VERSION.INCREMENTAL.contains("Flyme_OS_4")
|| Pattern.compile("Flyme OS [4|5]", Pattern.CASE_INSENSITIVE).matcher(Build.DISPLAY).find();
}
/**
* 判断是否为MIUI6以上
*/
public static boolean isMIUI6Later() {
try {
Class<?> clz = Class.forName("android.os.SystemProperties");
Method mtd = clz.getMethod("get", String.class);
String val = (String) mtd.invoke(null, "ro.miui.ui.version.name");
val = val.replaceAll("[vV]", "");
int version = Integer.parseInt(val);
return version >= 6;
} catch (Exception e) {
return false;
}
}
//</editor-fold>
/**
* 增加View的paddingTop,增加的值为状态栏高度
*/
public static void setPadding(Context context, View view) {
if (Build.VERSION.SDK_INT >= MIN_API) {
view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + getStatusBarHeight(context),
view.getPaddingRight(), view.getPaddingBottom());
}
}
/**
* 增加View的paddingTop,增加的值为状态栏高度 (智能判断,并设置高度)
*/
public static void setPaddingSmart(Context context, View view) {
if (Build.VERSION.SDK_INT >= MIN_API) {
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp != null && lp.height > 0) {
lp.height += getStatusBarHeight(context);//增高
}
view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + getStatusBarHeight(context),
view.getPaddingRight(), view.getPaddingBottom());
}
}
/**
* 增加View的高度以及paddingTop,增加的值为状态栏高度.一般是在沉浸式全屏给ToolBar用的
*/
public static void setHeightAndPadding(Context context, View view) {
if (Build.VERSION.SDK_INT >= MIN_API) {
ViewGroup.LayoutParams lp = view.getLayoutParams();
lp.height += getStatusBarHeight(context);//增高
view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + getStatusBarHeight(context),
view.getPaddingRight(), view.getPaddingBottom());
}
}
/**
* 增加View上边距(MarginTop)一般是给高度为 WARP_CONTENT 的小控件用的
*/
public static void setMargin(Context context, View view) {
if (Build.VERSION.SDK_INT >= MIN_API) {
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp instanceof ViewGroup.MarginLayoutParams) {
((ViewGroup.MarginLayoutParams) lp).topMargin += getStatusBarHeight(context);//增高
}
view.setLayoutParams(lp);
}
}
/**
* 创建假的透明栏
*/
public static void setTranslucentView(ViewGroup container, int color, @FloatRange(from = 0.0, to = 1.0) float alpha) {
if (Build.VERSION.SDK_INT >= 19) {
int mixtureColor = mixtureColor(color, alpha);
View translucentView = container.findViewById(android.R.id.custom);
if (translucentView == null && mixtureColor != 0) {
translucentView = new View(container.getContext());
translucentView.setId(android.R.id.custom);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(container.getContext()));
container.addView(translucentView, lp);
}
if (translucentView != null) {
translucentView.setBackgroundColor(mixtureColor);
}
}
}
public static int mixtureColor(int color, @FloatRange(from = 0.0, to = 1.0) float alpha) {
int a = (color & 0xff000000) == 0 ? 0xff : color >>> 24;
return (color & 0x00ffffff) | (((int) (a * alpha)) << 24);
}
/**
* 获取状态栏高度
*/
public static int getStatusBarHeight(Context context) {
int result = 24;
int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resId > 0) {
result = context.getResources().getDimensionPixelSize(resId);
} else {
result = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
result, Resources.getSystem().getDisplayMetrics());
}
return result;
}
/**
* 修改状态栏为全透明
*
* @param activity
*/
@TargetApi(19)
public static void transparencyBar(Activity activity) {
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) {
Window window = activity.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
/**
* 修改状态栏颜色,支持4.4以上版本
*
* @param activity
* @param colorId
*/
// public static void setStatusBarColor(Activity activity, int colorId) {
//
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// window.setStatusBarColor(activity.getResources().getColor(colorId));
// } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// //使用SystemBarTint库使4.4版本状态栏变色,需要先将状态栏设置为透明
// transparencyBar(activity);
// SystemBarTintManager tintManager = new SystemBarTintManager(activity);
// tintManager.setStatusBarTintEnabled(true);
// tintManager.setStatusBarTintResource(colorId);
// }
// }
/**
* 设置状态栏黑色字体图标,
* 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
*
* @param activity
* @return 1:MIUUI 2:Flyme 3:android6.0
*/
public static int StatusBarLightMode(Activity activity) {
int result = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (MIUISetStatusBarLightMode(activity.getWindow(), true)) {
result = 1;
} else if (FlymeSetStatusBarLightMode(activity.getWindow(), true)) {
result = 2;
} else 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);
result = 3;
}
}
return result;
}
/**
* 已知系统类型时,设置状态栏黑色字体图标。
* 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
*
* @param activity
* @param type 1:MIUUI 2:Flyme 3:android6.0
*/
public static void StatusBarLightMode(Activity activity, int type) {
if (type == 1) {
MIUISetStatusBarLightMode(activity.getWindow(), true);
} else if (type == 2) {
FlymeSetStatusBarLightMode(activity.getWindow(), true);
} else if (type == 3) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
/**
* 清除MIUI或flyme或6.0以上版本状态栏黑色字体
*/
public static void StatusBarDarkMode(Activity activity, int type) {
if (type == 1) {
MIUISetStatusBarLightMode(activity.getWindow(), false);
} else if (type == 2) {
FlymeSetStatusBarLightMode(activity.getWindow(), false);
} else if (type == 3) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}
/**
* 设置状态栏图标为深色和魅族特定的文字风格
* 可以用来判断是否为Flyme用户
*
* @param window 需要设置的窗口
* @param dark 是否把状态栏字体及图标颜色设置为深色
* @return boolean 成功执行返回true
*/
public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {
boolean result = false;
if (window != null) {
try {
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);
result = true;
} catch (Exception e) {
}
}
return result;
}
/**
* 设置状态栏字体图标为深色,需要MIUIV6以上
*
* @param window 需要设置的窗口
* @param dark 是否把状态栏字体及图标颜色设置为深色
* @return boolean 成功执行返回true
*/
public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) {
boolean result = false;
if (window != null) {
Class clazz = window.getClass();
try {
int darkModeFlag = 0;
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
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)
;//清除黑色字体
}
result = true;
} catch (Exception e) {
}
}
return result;
}
}
有需要的小伙伴直接复制过去使用吧。
当然也可以下载资源。