在Android4.4之前,我们的应用没法改变手机的状态栏颜色,当我们打开应用时,会出现上图中左侧的画面,在屏幕的顶部有一条黑色的状态栏,和应用的风格非常不协调;为了提供更好的界面交互,google在Android4.4以后提供了设置沉浸式状态栏的方法,对于沉浸式状态栏这个名字存在争议,我不做讨论,实际的效果其实就是透明的状态栏,然后在状态栏的位置显示我们自定义的颜色,通常为应用的actionbar的颜色,或者是将应用的整体的一张图片也占据到状态栏中,如下图所示:
沉浸式状态栏的实现
方法一:通过设置 Theme 主题设置状态栏透明
因为 API21 之后(也就是 android 5.0 之后)的状态栏,会默认覆盖一层半透明遮罩。且为了保持4.4以前系统正常使用,故需要三份style文件,即默认的values(不设置状态栏透明)、values-v19、values-v21(解决半透明遮罩问题)。
在values下的style中加入如下代码:
// values-v19。v19 开始有 android:windowTranslucentStatus 这个属性
<resources>
<!-- Base application theme. -->
<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>
<!--values-v19。v19 开始有 android:windowTranslucentStatus 这个属性-->
<item name="android:windowTranslucentStatus">true</item>
<!--透明导航栏-->
<item name="android:windowTranslucentNavigation">true</item>
<!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>
图片中我们发现,虽然我们实现了透明的状态栏,但是上边的文字和状态栏的信息重叠了。为了修复这个问题,我们应该在布局文件的根布局中加入
android:fitsSystemWindows="true"
或者在主题中加入
<item name="android:fitsSystemWindows">true</item>
就可以了,加入后效果如下:
需要注意的几点:
- 为Android4.4+适配的同时,不要忘了在values的style.xml中加入同名的自定义style,不然4.4以下会报错
- 不要忘记在布局文件中加入 android:fitsSystemWindows=”true”,不然布局内容会和状态栏内容重叠
- 在Android5.0中,透明标题的效果和4.4中有所不同,但是实现方法相同,我们可以不必为5.0+单独适配,同时5.0+的版本提供了新的API android:statusBarColor,可以用来单独设置状态栏的颜色,但是这种方法不适用于我们例子中的图片占据状态栏,它适用于纯色的状态栏,并且它应该放到value-v21文件夹中
方法二:通过代码实现
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
//或者,只不过,这个需要API版本大于21才行,也就是Android 5.0以上
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTranslucentStatus();//状态栏隐藏
private void initTranslucentStatus() {
//4.4 全透明状态栏(有的机子是过渡形式的透明)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
//5.0 全透明实现
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = 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);// calculateStatusColor(Color.WHITE, (int) alphaValue)
}
}
状态栏直接透明,但是会出现内容区域顶上去的现象,这里,还是需要加上下面这行代码
android:fitsSystemWindows="true"
或者在主题中加入
<item name="android:fitsSystemWindows">true</item>
全屏状态栏的实现
方法一:使用Theme实现
<resources>
<!-- Base application theme. -->
<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>
<!--全屏状态栏-->
<item name="android:windowFullscreen">true</item>
</style>
</resources>
方法二:通过代码实现
//重写通过onWindowFocusChanged方法实现,也算是沉浸式状态栏
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && Build.VERSION.SDK_INT >= 19) {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
定义状态栏颜色
调整4.4的显示方案:
4.4之后加入windowTranslucentStatus的属性之后,也就是我们可以用到状态栏的区域了。
既然我们可以用到这块区域,那么我们只要在根布局去设置一个与状态栏等高的View,设置背景色为我们期望的颜色就可以了。**
这里,使用一个工具类来定义
public class StatusBarCompat {
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;
}
}
在代码中直接使用
StatusBarCompat.compat(this);
//或者自己设置状态看颜色,那么就用这个方法:
StatusBarCompat.compat(this, getResources().getColor(R.color.colorAccent));
或者一个更厉害的工具类
/**
* 状态栏透明
* Created by SCWANG on 2016/10/26.
*/
@SuppressWarnings("unused")
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);
}
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) {
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);
}
}
//</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;
}
}
使用方法几乎一样!
StatusBarUtil.immersive(this, 0x03A9F4, 1f);