下面列出了android.view.View#getSystemUiVisibility ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 设置 Navigation Bar 是否高亮模式
* @param window {@link Window}
* @param isLightMode 是否高亮模式
* @return {@code true} success, {@code false} fail
*/
public static boolean setNavBarLightMode(final Window window, final boolean isLightMode) {
if (window != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
View decorView = window.getDecorView();
int vis = decorView.getSystemUiVisibility();
if (isLightMode) {
vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
} else {
vis &= ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
}
decorView.setSystemUiVisibility(vis);
return true;
}
return false;
}
/**
* 设置状态栏颜色
*
* @param view
* @param dark
*/
private static void setStatusBarDarkIcon(View view, boolean dark) {
int oldVis = view.getSystemUiVisibility();
int newVis = oldVis;
if (dark) {
newVis |= SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
} else {
newVis &= ~SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
if (newVis != oldVis) {
view.setSystemUiVisibility(newVis);
}
}
/**
* 获取 Navigation Bar 是否高亮模式
* @param window {@link Window}
* @return {@code true} yes, {@code false} no
*/
public static boolean isNavBarLightMode(final Window window) {
if (window != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
View decorView = window.getDecorView();
int vis = decorView.getSystemUiVisibility();
return (vis & View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR) != 0;
}
return false;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void setLightStatusBar(Window window, boolean lightStatusBar) {
// 设置浅色状态栏时的界面显示
View decor = window.getDecorView();
int ui = decor.getSystemUiVisibility();
if (lightStatusBar) {
ui |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
} else {
ui &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
decor.setSystemUiVisibility(ui);
}
public static void clearLightStatusBar(@NonNull View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int flags = view.getSystemUiVisibility();
flags &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
view.setSystemUiVisibility(flags);
}
}
/**
* Sets activity's decor view into an immersive mode.
* If immersive mode is not supported, this method no-ops.
*/
private void enterImmersiveMode() {
// Immersive mode is only supported in API 19+.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
if (mSetImmersiveRunnable == null) {
final View decor = getWindow().getDecorView();
mSetImmersiveRunnable = new Runnable() {
@Override
public void run() {
int currentFlags = decor.getSystemUiVisibility();
int desiredFlags = currentFlags | IMMERSIVE_MODE_UI_FLAGS;
if (currentFlags != desiredFlags) {
decor.setSystemUiVisibility(desiredFlags);
}
}
};
// When we enter immersive mode for the first time, register a
// SystemUiVisibilityChangeListener that restores immersive mode. This is necessary
// because user actions like focusing a keyboard will break out of immersive mode.
decor.setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int newFlags) {
if ((newFlags & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
asyncSetImmersive(RESTORE_IMMERSIVE_MODE_DELAY_MILLIS);
}
}
});
}
asyncSetImmersive(0);
}
/**
* Set the status bar's light mode.
*
* @param window The window.
* @param isLightMode True to set status bar light mode, false otherwise.
*/
public static void setStatusBarLightMode(@NonNull final Window window,
final boolean isLightMode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View decorView = window.getDecorView();
int vis = decorView.getSystemUiVisibility();
if (isLightMode) {
vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
} else {
vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
decorView.setSystemUiVisibility(vis);
}
}
public static void setLightNavigationBar(Window window, boolean enable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final View decorView = window.getDecorView();
int flags = decorView.getSystemUiVisibility();
if (enable) {
flags |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
} else {
flags &= ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
}
decorView.setSystemUiVisibility(flags);
}
}
public static void setNavigationBarHidden(Window window, boolean hidden) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View decorView = window.getDecorView();
int systemUi = decorView.getSystemUiVisibility();
if (hidden) {
systemUi |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
systemUi |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
} else {
systemUi &= ~View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
systemUi &= ~View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
}
window.getDecorView().setSystemUiVisibility(systemUi);
}
}
/**
* Is the nav bar light mode.
*
* @param window The window.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isNavBarLightMode(@NonNull final Window window) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
View decorView = window.getDecorView();
int vis = decorView.getSystemUiVisibility();
return (vis & View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR) != 0;
}
return false;
}
/**
* 设置状态栏颜色
*
* @param view
* @param dark
*/
private static void setStatusBarDarkIcon(View view, boolean dark) {
int oldVis = view.getSystemUiVisibility();
int newVis = oldVis;
if (dark) {
newVis |= SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
} else {
newVis &= ~SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
if (newVis != oldVis) {
view.setSystemUiVisibility(newVis);
}
}
/**
* Set light status bar if we are using light primary color on API 23 and above devices.
*
* @param view The view to get the system ui flags.
* @param light {@code true} to set the light status bar.
*/
@TargetApi(Build.VERSION_CODES.M)
public static void setLightStatusBar(@NonNull View view, boolean light) {
if (DynamicSdkUtils.is23()) {
int flags = view.getSystemUiVisibility();
if (light) {
flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
} else {
flags &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
view.setSystemUiVisibility(flags);
}
}
public static boolean checkFullScreenByCode(Context context) {
if (context instanceof Activity) {
Window window = ((Activity) context).getWindow();
if (window != null) {
View decorView = window.getDecorView();
if (decorView != null) {
return (decorView.getSystemUiVisibility() & View.SYSTEM_UI_FLAG_FULLSCREEN) == View.SYSTEM_UI_FLAG_FULLSCREEN;
}
}
}
return false;
}
/**
* 设置状态栏颜色
*
* @param view
* @param dark
*/
private static void setStatusBarDarkIcon(View view, boolean dark) {
int oldVis = view.getSystemUiVisibility();
int newVis = oldVis;
if (dark) {
newVis |= SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
} else {
newVis &= ~SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
if (newVis != oldVis) {
view.setSystemUiVisibility(newVis);
}
}
public static void enableLightStatusBar(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View view = activity.getWindow().getDecorView();
int flags = view.getSystemUiVisibility();
flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
view.setSystemUiVisibility(flags);
}
}
private void setStatusBarVisible(Bool visible) {
View decorView = activity.getWindow().getDecorView();
int flags = decorView.getSystemUiVisibility();
if (visible.isFalse()) {
flags |= View.SYSTEM_UI_FLAG_FULLSCREEN;
} else {
flags &= ~View.SYSTEM_UI_FLAG_FULLSCREEN;
}
decorView.setSystemUiVisibility(flags);
}
public static boolean hasFlags(View view, int flags) {
return (view.getSystemUiVisibility() & flags) == flags;
}
private static void addSystemUiVisibilityFlag(Activity activity, int flag) {
View root = activity.getWindow().getDecorView().getRootView();
int visibility = root.getSystemUiVisibility();
visibility |= flag;
root.setSystemUiVisibility(visibility);
}
private boolean getSystemUiVisibilityFlagValue(int flag, boolean defaultValue) {
View v = mWindow == null ? null : mWindow.getDecorView();
return v == null ? defaultValue : (v.getSystemUiVisibility() & flag) != 0;
}
private static void showNavigationBar(Context context) {
View decorView = scanForActivity(context).getWindow().getDecorView();
int systemUiVisibility = decorView.getSystemUiVisibility();
systemUiVisibility &= ~FLAGS;
decorView.setSystemUiVisibility(systemUiVisibility);
}