java 状态栏工具类,显示隐藏状态栏,设置状态栏颜色,沉浸式,透明状态栏
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 状态栏工具类,显示隐藏状态栏,设置状态栏颜色,沉浸式,透明状态栏相关的知识,希望对你有一定的参考价值。
public class NavigationUtils {
private static int mNavigationbarColor = Color.BLACK;
// 显示状态栏
public static void showStatusbar(Window window) {
if (window == null) {
return;
}
if (isDeviceVersionBelowLollipop()) {
return;
}
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
// 隐藏状态栏
public static void hideStatusbar(Window window) {
if (window == null) {
return;
}
if (isDeviceVersionBelowLollipop()) {
return;
}
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
// 设置状态栏的背景色
public static void setStatusbarBgColor(Window window) {
if (window == null) {
return;
}
if (isDeviceVersionBelowLollipop()) {
return;
}
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.parseColor("#3F000000"));
}
// 导航栏变成透明的
public static void hideNavigationbar(Window window) {
if (window == null) {
return;
}
if (isDeviceVersionBelowLollipop()) {
return;
}
if (isDeviceVersionBelowM()) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
} else {
mNavigationbarColor = window.getNavigationBarColor();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setNavigationBarColor(Color.TRANSPARENT);
}
}
// 取消导航栏透明
public static void showNavigationbar(Window window) {
if (window == null) {
return;
}
if (isDeviceVersionBelowLollipop()) {
return;
}
if (isDeviceVersionBelowM()) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setNavigationBarColor(mNavigationbarColor);
}
}
public static void hideNaviAndStatusBar(Window window) {
hideNavigationbar(window);
hideStatusbar(window);
}
public static void showNaviAndStatusBar(Window window) {
showNavigationbar(window);
showStatusbar(window);
}
private static boolean isDeviceVersionBelowLollipop() {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP;
}
private static boolean isDeviceVersionBelowM() {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M;
}
// 设置view可以放到navigation的下面
public static void translucentNavigationLayout(Window window) {
if (window == null) {
return;
}
if (isDeviceVersionBelowLollipop()) {
return;
}
View decorView = window.getDecorView();
int uiFlag = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(uiFlag);
}
/**
* 使用沉浸式模式,导航栏和状态栏都隐藏
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static boolean useImmersiveMode(Window window) {
if (window == null) {
return false;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return false;
}
// 透明状态栏
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// 透明导航栏
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView()
.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);
return true;
}
}
以上是关于java 状态栏工具类,显示隐藏状态栏,设置状态栏颜色,沉浸式,透明状态栏的主要内容,如果未能解决你的问题,请参考以下文章