parent
9b5bbbb1de
commit
41665bb037
6 changed files with 62 additions and 803 deletions
@ -1,734 +0,0 @@ |
||||
package com.project.survey.util; |
||||
|
||||
import static android.os.Build.VERSION_CODES.HONEYCOMB; |
||||
import static android.os.Build.VERSION_CODES.KITKAT; |
||||
|
||||
import android.annotation.TargetApi; |
||||
import android.app.Activity; |
||||
import android.app.Dialog; |
||||
import android.content.Context; |
||||
import android.graphics.Color; |
||||
import android.os.Build; |
||||
import android.view.Gravity; |
||||
import android.view.View; |
||||
import android.view.ViewGroup; |
||||
import android.view.Window; |
||||
import android.view.WindowManager; |
||||
import android.widget.FrameLayout; |
||||
|
||||
import androidx.annotation.ColorInt; |
||||
import androidx.annotation.IntDef; |
||||
import androidx.core.view.ViewCompat; |
||||
|
||||
import com.project.survey.widget.util.Utils; |
||||
|
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.reflect.Field; |
||||
import java.lang.reflect.Method; |
||||
|
||||
/** |
||||
* 状态栏工具 |
||||
* |
||||
* @author XUE |
||||
* @since 2019/3/22 10:50 |
||||
*/ |
||||
public class StatusBarUtils { |
||||
|
||||
private final static int STATUSBAR_TYPE_DEFAULT = 0; |
||||
private final static int STATUSBAR_TYPE_MIUI = 1; |
||||
private final static int STATUSBAR_TYPE_FLYME = 2; |
||||
private final static int STATUSBAR_TYPE_ANDROID6 = 3; // Android 6.0
|
||||
private final static int STATUS_BAR_DEFAULT_HEIGHT_DP = 25; // 大部分状态栏都是25dp
|
||||
// 在某些机子上存在不同的density值,所以增加两个虚拟值
|
||||
public static float sVirtualDensity = -1; |
||||
public static float sVirtualDensityDpi = -1; |
||||
private static int sStatusbarHeight = -1; |
||||
private static @StatusBarType |
||||
int mStatusBarType = STATUSBAR_TYPE_DEFAULT; |
||||
private static Integer sTransparentValue; |
||||
|
||||
public static void translucent(Activity activity) { |
||||
translucent(activity.getWindow()); |
||||
} |
||||
|
||||
public static void translucent(Window window) { |
||||
translucent(window, 0x40000000); |
||||
} |
||||
|
||||
private static boolean supportTranslucent() { |
||||
return Build.VERSION.SDK_INT >= KITKAT |
||||
// Essential Phone 在 Android 8 之前沉浸式做得不全,系统不从状态栏顶部开始布局却会下发 WindowInsets
|
||||
&& !(DeviceUtils.isEssentialPhone() && Build.VERSION.SDK_INT < 26); |
||||
} |
||||
|
||||
private StatusBarUtils() { |
||||
throw new UnsupportedOperationException("u can't instantiate me..."); |
||||
} |
||||
|
||||
/** |
||||
* 设置沉浸式状态栏样式 |
||||
* |
||||
* @param activity |
||||
* @param isDark 是否是深色的状态栏 |
||||
*/ |
||||
public static void initStatusBarStyle(Activity activity, boolean isDark) { |
||||
initStatusBarStyle(activity, isDark, Color.TRANSPARENT); |
||||
} |
||||
|
||||
/** |
||||
* 设置沉浸式状态栏样式 |
||||
* |
||||
* @param activity |
||||
* @param isDark 是否是深色的状态栏 |
||||
* @param colorOn5x 颜色 |
||||
*/ |
||||
public static void initStatusBarStyle(Activity activity, boolean isDark, @ColorInt int colorOn5x) { |
||||
//设置沉浸式状态栏的颜色
|
||||
translucent(activity, colorOn5x); |
||||
//修改状态栏的字体颜色
|
||||
if (isDark) { |
||||
setStatusBarDarkMode(activity); |
||||
} else { |
||||
setStatusBarLightMode(activity); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 沉浸式状态栏。 |
||||
* 支持 4.4 以上版本的 MIUI 和 Flyme,以及 5.0 以上版本的其他 Android。 |
||||
* |
||||
* @param activity 需要被设置沉浸式状态栏的 Activity。 |
||||
*/ |
||||
public static void translucent(Activity activity, @ColorInt int colorOn5x) { |
||||
Window window = activity.getWindow(); |
||||
translucent(window, colorOn5x); |
||||
} |
||||
|
||||
@TargetApi(Build.VERSION_CODES.KITKAT) |
||||
public static void translucent(Window window, @ColorInt int colorOn5x) { |
||||
if (!supportTranslucent()) { |
||||
// 版本小于4.4,绝对不考虑沉浸式
|
||||
return; |
||||
} |
||||
|
||||
if (isNotchOfficialSupport()) { |
||||
handleDisplayCutoutMode(window); |
||||
} |
||||
|
||||
// 小米和魅族4.4 以上版本支持沉浸式
|
||||
// 小米 Android 6.0 ,开发版 7.7.13 及以后版本设置黑色字体又需要 clear FLAG_TRANSLUCENT_STATUS, 因此还原为官方模式
|
||||
if (DeviceUtils.isMeizu() || (DeviceUtils.isMIUI() && Build.VERSION.SDK_INT < Build.VERSION_CODES.M)) { |
||||
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, |
||||
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); |
||||
return; |
||||
} |
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
||||
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
||||
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && supportTransclentStatusBar6()) { |
||||
// android 6以后可以改状态栏字体颜色,因此可以自行设置为透明
|
||||
// ZUK Z1是个另类,自家应用可以实现字体颜色变色,但没开放接口
|
||||
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); |
||||
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
||||
window.setStatusBarColor(Color.TRANSPARENT); |
||||
} else { |
||||
// android 5不能修改状态栏字体颜色,因此直接用FLAG_TRANSLUCENT_STATUS,nexus表现为半透明
|
||||
// 魅族和小米的表现如何?
|
||||
// update: 部分手机运用FLAG_TRANSLUCENT_STATUS时背景不是半透明而是没有背景了。。。。。
|
||||
// window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
|
||||
|
||||
// 采取setStatusBarColor的方式,部分机型不支持,那就纯黑了,保证状态栏图标可见
|
||||
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); |
||||
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
||||
window.setStatusBarColor(colorOn5x); |
||||
} |
||||
// } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
// // android4.4的默认是从上到下黑到透明,我们的背景是白色,很难看,因此只做魅族和小米的
|
||||
// } else if(Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1){
|
||||
// // 如果app 为白色,需要更改状态栏颜色,因此不能让19一下支持透明状态栏
|
||||
// Window window = activity.getWindow();
|
||||
// Integer transparentValue = getStatusBarAPITransparentValue(activity);
|
||||
// if(transparentValue != null) {
|
||||
// window.getDecorView().setSystemUiVisibility(transparentValue);
|
||||
// }
|
||||
} |
||||
} |
||||
|
||||
public static boolean isNotchOfficialSupport() { |
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.P; |
||||
} |
||||
|
||||
@TargetApi(Build.VERSION_CODES.P) |
||||
private static void handleDisplayCutoutMode(final Window window) { |
||||
View decorView = window.getDecorView(); |
||||
if (decorView != null) { |
||||
if (ViewCompat.isAttachedToWindow(decorView)) { |
||||
realHandleDisplayCutoutMode(window, decorView); |
||||
} else { |
||||
decorView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() { |
||||
@Override |
||||
public void onViewAttachedToWindow(View v) { |
||||
v.removeOnAttachStateChangeListener(this); |
||||
realHandleDisplayCutoutMode(window, v); |
||||
} |
||||
|
||||
@Override |
||||
public void onViewDetachedFromWindow(View v) { |
||||
|
||||
} |
||||
}); |
||||
} |
||||
} |
||||
} |
||||
|
||||
@TargetApi(Build.VERSION_CODES.P) |
||||
private static void realHandleDisplayCutoutMode(Window window, View decorView) { |
||||
if (decorView.getRootWindowInsets() != null && |
||||
decorView.getRootWindowInsets().getDisplayCutout() != null) { |
||||
WindowManager.LayoutParams params = window.getAttributes(); |
||||
params.layoutInDisplayCutoutMode = WindowManager.LayoutParams |
||||
.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; |
||||
window.setAttributes(params); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 设置状态栏黑色字体图标, |
||||
* 支持 4.4 以上版本 MIUI 和 Flyme,以及 6.0 以上版本的其他 Android |
||||
* |
||||
* @param activity 需要被处理的 Activity |
||||
*/ |
||||
public static boolean setStatusBarLightMode(Activity activity) { |
||||
if (activity == null) { |
||||
return false; |
||||
} |
||||
// 无语系列:ZTK C2016只能时间和电池图标变色。。。。
|
||||
if (DeviceUtils.isZTKC2016()) { |
||||
return false; |
||||
} |
||||
|
||||
if (mStatusBarType != STATUSBAR_TYPE_DEFAULT) { |
||||
return setStatusBarLightMode(activity, mStatusBarType); |
||||
} |
||||
if (Build.VERSION.SDK_INT >= KITKAT) { |
||||
if (isMIUICustomStatusBarLightModeImpl() && MIUISetStatusBarLightMode(activity.getWindow(), true)) { |
||||
mStatusBarType = STATUSBAR_TYPE_MIUI; |
||||
return true; |
||||
} else if (FlymeSetStatusBarLightMode(activity.getWindow(), true)) { |
||||
mStatusBarType = STATUSBAR_TYPE_FLYME; |
||||
return true; |
||||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
||||
Android6SetStatusBarLightMode(activity.getWindow(), true); |
||||
mStatusBarType = STATUSBAR_TYPE_ANDROID6; |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 已知系统类型时,设置状态栏黑色字体图标。 |
||||
* 支持 4.4 以上版本 MIUI 和 Flyme,以及 6.0 以上版本的其他 Android |
||||
* |
||||
* @param activity 需要被处理的 Activity |
||||
* @param type StatusBar 类型,对应不同的系统 |
||||
*/ |
||||
private static boolean setStatusBarLightMode(Activity activity, @StatusBarType int type) { |
||||
if (type == STATUSBAR_TYPE_MIUI) { |
||||
return MIUISetStatusBarLightMode(activity.getWindow(), true); |
||||
} else if (type == STATUSBAR_TYPE_FLYME) { |
||||
return FlymeSetStatusBarLightMode(activity.getWindow(), true); |
||||
} else if (type == STATUSBAR_TYPE_ANDROID6) { |
||||
return Android6SetStatusBarLightMode(activity.getWindow(), true); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 设置状态栏白色字体图标 |
||||
* 支持 4.4 以上版本 MIUI 和 Flyme,以及 6.0 以上版本的其他 Android |
||||
*/ |
||||
public static boolean setStatusBarDarkMode(Activity activity) { |
||||
if (activity == null) { |
||||
return false; |
||||
} |
||||
if (mStatusBarType == STATUSBAR_TYPE_DEFAULT) { |
||||
// 默认状态,不需要处理
|
||||
return true; |
||||
} |
||||
|
||||
if (mStatusBarType == STATUSBAR_TYPE_MIUI) { |
||||
return MIUISetStatusBarLightMode(activity.getWindow(), false); |
||||
} else if (mStatusBarType == STATUSBAR_TYPE_FLYME) { |
||||
return FlymeSetStatusBarLightMode(activity.getWindow(), false); |
||||
} else if (mStatusBarType == STATUSBAR_TYPE_ANDROID6) { |
||||
return Android6SetStatusBarLightMode(activity.getWindow(), false); |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
@TargetApi(Build.VERSION_CODES.M) |
||||
private static int changeStatusBarModeRetainFlag(Window window, int out) { |
||||
out = retainSystemUiFlag(window, out, View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); |
||||
out = retainSystemUiFlag(window, out, View.SYSTEM_UI_FLAG_FULLSCREEN); |
||||
out = retainSystemUiFlag(window, out, View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); |
||||
out = retainSystemUiFlag(window, out, View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); |
||||
out = retainSystemUiFlag(window, out, View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); |
||||
out = retainSystemUiFlag(window, out, View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); |
||||
return out; |
||||
} |
||||
|
||||
public static int retainSystemUiFlag(Window window, int out, int type) { |
||||
int now = window.getDecorView().getSystemUiVisibility(); |
||||
if ((now & type) == type) { |
||||
out |= type; |
||||
} |
||||
return out; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 设置状态栏字体图标为深色,Android 6 |
||||
* |
||||
* @param window 需要设置的窗口 |
||||
* @param light 是否把状态栏字体及图标颜色设置为深色 |
||||
* @return boolean 成功执行返回true |
||||
*/ |
||||
@TargetApi(Build.VERSION_CODES.M) |
||||
private static boolean Android6SetStatusBarLightMode(Window window, boolean light) { |
||||
View decorView = window.getDecorView(); |
||||
int systemUi = light ? View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR : View.SYSTEM_UI_FLAG_LAYOUT_STABLE; |
||||
systemUi = changeStatusBarModeRetainFlag(window, systemUi); |
||||
decorView.setSystemUiVisibility(systemUi); |
||||
if (DeviceUtils.isMIUIV9()) { |
||||
// MIUI 9 低于 6.0 版本依旧只能回退到以前的方案
|
||||
// https://github.com/Tencent/QMUI_Android/issues/160
|
||||
MIUISetStatusBarLightMode(window, light); |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
/** |
||||
* 设置状态栏字体图标为深色,需要 MIUIV6 以上 |
||||
* |
||||
* @param window 需要设置的窗口 |
||||
* @param light 是否把状态栏字体及图标颜色设置为深色 |
||||
* @return boolean 成功执行返回 true |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
public static boolean MIUISetStatusBarLightMode(Window window, boolean light) { |
||||
boolean result = false; |
||||
if (window != null) { |
||||
Class clazz = window.getClass(); |
||||
try { |
||||
int darkModeFlag; |
||||
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 (light) { |
||||
extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
|
||||
} else { |
||||
extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
|
||||
} |
||||
result = true; |
||||
} catch (Exception ignored) { |
||||
|
||||
} |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* 更改状态栏图标、文字颜色的方案是否是MIUI自家的, MIUI9 && Android 6 之后用回Android原生实现 |
||||
* 见小米开发文档说明:https://dev.mi.com/console/doc/detail?pId=1159
|
||||
*/ |
||||
private static boolean isMIUICustomStatusBarLightModeImpl() { |
||||
if (DeviceUtils.isMIUIV9() && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { |
||||
return true; |
||||
} |
||||
return DeviceUtils.isMIUIV5() || DeviceUtils.isMIUIV6() || |
||||
DeviceUtils.isMIUIV7() || DeviceUtils.isMIUIV8(); |
||||
} |
||||
|
||||
/** |
||||
* 设置状态栏图标为深色和魅族特定的文字风格 |
||||
* 可以用来判断是否为 Flyme 用户 |
||||
* |
||||
* @param window 需要设置的窗口 |
||||
* @param light 是否把状态栏字体及图标颜色设置为深色 |
||||
* @return boolean 成功执行返回true |
||||
*/ |
||||
public static boolean FlymeSetStatusBarLightMode(Window window, boolean light) { |
||||
boolean result = false; |
||||
if (window != null) { |
||||
// flyme 在 6.2.0.0A 支持了 Android 官方的实现方案,旧的方案失效
|
||||
Android6SetStatusBarLightMode(window, light); |
||||
|
||||
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 (light) { |
||||
value |= bit; |
||||
} else { |
||||
value &= ~bit; |
||||
} |
||||
meizuFlags.setInt(lp, value); |
||||
window.setAttributes(lp); |
||||
result = true; |
||||
} catch (Exception ignored) { |
||||
|
||||
} |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* 获取是否全屏 |
||||
* |
||||
* @return 是否全屏 |
||||
*/ |
||||
public static boolean isFullScreen(Activity activity) { |
||||
boolean ret = false; |
||||
try { |
||||
WindowManager.LayoutParams attrs = activity.getWindow().getAttributes(); |
||||
ret = (attrs.flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0; |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return ret; |
||||
} |
||||
|
||||
/** |
||||
* API19之前透明状态栏:获取设置透明状态栏的system ui visibility的值,这是部分有提供接口的rom使用的 |
||||
* http://stackoverflow.com/questions/21865621/transparent-status-bar-before-4-4-kitkat
|
||||
*/ |
||||
public static Integer getStatusBarAPITransparentValue(Context context) { |
||||
if (sTransparentValue != null) { |
||||
return sTransparentValue; |
||||
} |
||||
String[] systemSharedLibraryNames = context.getPackageManager() |
||||
.getSystemSharedLibraryNames(); |
||||
String fieldName = null; |
||||
for (String lib : systemSharedLibraryNames) { |
||||
if ("touchwiz".equals(lib)) { |
||||
fieldName = "SYSTEM_UI_FLAG_TRANSPARENT_BACKGROUND"; |
||||
} else if (lib.startsWith("com.sonyericsson.navigationbar")) { |
||||
fieldName = "SYSTEM_UI_FLAG_TRANSPARENT"; |
||||
} |
||||
} |
||||
|
||||
if (fieldName != null) { |
||||
try { |
||||
Field field = View.class.getField(fieldName); |
||||
if (field != null) { |
||||
Class<?> type = field.getType(); |
||||
if (type == int.class) { |
||||
sTransparentValue = field.getInt(null); |
||||
} |
||||
} |
||||
} catch (Exception ignored) { |
||||
} |
||||
} |
||||
return sTransparentValue; |
||||
} |
||||
|
||||
/** |
||||
* 检测 Android 6.0 是否可以启用 window.setStatusBarColor(Color.TRANSPARENT)。 |
||||
*/ |
||||
public static boolean supportTransclentStatusBar6() { |
||||
return !(DeviceUtils.isZUKZ1() || DeviceUtils.isZTKC2016()); |
||||
} |
||||
|
||||
/** |
||||
* 获取状态栏的高度。 |
||||
* |
||||
* @param context 上下文 |
||||
* @return 状态栏高度 |
||||
*/ |
||||
public static int getStatusBarHeight(Context context) { |
||||
if (sStatusbarHeight == -1) { |
||||
sStatusbarHeight = Utils.getStatusBarHeight(context); |
||||
} |
||||
return sStatusbarHeight; |
||||
} |
||||
|
||||
public static void setVirtualDensity(float density) { |
||||
sVirtualDensity = density; |
||||
} |
||||
|
||||
public static void setVirtualDensityDpi(float densityDpi) { |
||||
sVirtualDensityDpi = densityDpi; |
||||
} |
||||
|
||||
@IntDef({STATUSBAR_TYPE_DEFAULT, STATUSBAR_TYPE_MIUI, STATUSBAR_TYPE_FLYME, STATUSBAR_TYPE_ANDROID6}) |
||||
@Retention(RetentionPolicy.SOURCE) |
||||
private @interface StatusBarType { |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 全屏 |
||||
* |
||||
* @param activity 窗口 |
||||
*/ |
||||
public static void fullScreen(Activity activity) { |
||||
if (activity == null) { |
||||
return; |
||||
} |
||||
fullScreen(activity.getWindow()); |
||||
} |
||||
|
||||
/** |
||||
* 全屏 |
||||
* |
||||
* @param window 窗口 |
||||
*/ |
||||
public static void fullScreen(Window window) { |
||||
if (window == null) { |
||||
return; |
||||
} |
||||
if (Build.VERSION.SDK_INT > HONEYCOMB && Build.VERSION.SDK_INT < KITKAT) { // lower api
|
||||
window.getDecorView().setSystemUiVisibility(View.GONE); |
||||
} else if (Build.VERSION.SDK_INT >= KITKAT) { |
||||
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE |
||||
| View.SYSTEM_UI_FLAG_FULLSCREEN |
||||
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
||||
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
||||
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 取消全屏 |
||||
* |
||||
* @param activity 窗口 |
||||
* @param statusBarColor 状态栏的颜色 |
||||
* @param navigationBarColor 导航栏的颜色 |
||||
*/ |
||||
public static void cancelFullScreen(Activity activity, @ColorInt int statusBarColor, @ColorInt int navigationBarColor) { |
||||
if (activity == null) { |
||||
return; |
||||
} |
||||
cancelFullScreen(activity.getWindow(), statusBarColor, navigationBarColor); |
||||
} |
||||
|
||||
/** |
||||
* 取消全屏 |
||||
* |
||||
* @param window 窗口 |
||||
* @param statusBarColor 状态栏的颜色 |
||||
* @param navigationBarColor 导航栏的颜色 |
||||
*/ |
||||
public static void cancelFullScreen(Window window, @ColorInt int statusBarColor, @ColorInt int navigationBarColor) { |
||||
if (window == null) { |
||||
return; |
||||
} |
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
||||
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS |
||||
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); |
||||
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); |
||||
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
||||
if (statusBarColor != -1) { |
||||
window.setStatusBarColor(statusBarColor); |
||||
} |
||||
if (navigationBarColor != -1) { |
||||
window.setNavigationBarColor(navigationBarColor); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 取消全屏 |
||||
* |
||||
* @param activity 窗口 |
||||
*/ |
||||
public static void cancelFullScreen(Activity activity) { |
||||
if (activity == null) { |
||||
return; |
||||
} |
||||
cancelFullScreen(activity.getWindow()); |
||||
} |
||||
|
||||
/** |
||||
* 取消全屏 |
||||
* |
||||
* @param window 窗口 |
||||
*/ |
||||
public static void cancelFullScreen(Window window) { |
||||
cancelFullScreen(window, -1, -1); |
||||
} |
||||
|
||||
/** |
||||
* 设置底部导航条的颜色 |
||||
* |
||||
* @param activity 窗口 |
||||
* @param color 颜色 |
||||
*/ |
||||
public static void setNavigationBarColor(Activity activity, int color) { |
||||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { |
||||
//5.0以上可以直接设置 navigation颜色
|
||||
activity.getWindow().setNavigationBarColor(color); |
||||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { |
||||
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); |
||||
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); |
||||
View navigationBar = new View(activity); |
||||
FrameLayout.LayoutParams params; |
||||
params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, getNavigationBarHeight(activity)); |
||||
params.gravity = Gravity.BOTTOM; |
||||
navigationBar.setLayoutParams(params); |
||||
navigationBar.setBackgroundColor(color); |
||||
decorView.addView(navigationBar); |
||||
} else { |
||||
//4.4以下无法设置NavigationBar颜色
|
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* 获取底部导航条的高度 |
||||
* |
||||
* @param context 上下文 |
||||
* @return 底部导航条的高度 |
||||
*/ |
||||
public static int getNavigationBarHeight(Context context) { |
||||
int height = 0; |
||||
int id = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android"); |
||||
if (id > 0) { |
||||
height = context.getResources().getDimensionPixelSize(id); |
||||
} |
||||
return height; |
||||
} |
||||
|
||||
/** |
||||
* 底部导航条是否显示 |
||||
* |
||||
* @param activity 窗口 |
||||
* @return 底部导航条是否显示 |
||||
*/ |
||||
public static boolean isNavigationBarExist(Activity activity) { |
||||
return DensityUtils.isNavigationBarExist(activity); |
||||
} |
||||
|
||||
/** |
||||
* 全屏下显示弹窗 |
||||
* |
||||
* @param dialog 弹窗 |
||||
*/ |
||||
public static void showDialogInFullScreen(final Dialog dialog) { |
||||
if (dialog == null) { |
||||
return; |
||||
} |
||||
showWindowInFullScreen(dialog.getWindow(), new IWindowShower() { |
||||
@Override |
||||
public void show(Window window) { |
||||
dialog.show(); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
/** |
||||
* 全屏下显示窗口【包括dialog等】 |
||||
* |
||||
* @param window 窗口 |
||||
* @param iWindowShower 窗口显示接口 |
||||
*/ |
||||
public static void showWindowInFullScreen(Window window, IWindowShower iWindowShower) { |
||||
if (window == null || iWindowShower == null) { |
||||
return; |
||||
} |
||||
window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); |
||||
iWindowShower.show(window); |
||||
StatusBarUtils.fullScreen(window); |
||||
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); |
||||
} |
||||
|
||||
/** |
||||
* 显示窗口【同步窗口系统view的可见度, 解决全屏下显示窗口导致界面退出全屏的问题】 |
||||
* |
||||
* @param activity 活动窗口 |
||||
* @param dialog 需要显示的窗口 |
||||
*/ |
||||
public static void showDialog(Activity activity, final Dialog dialog) { |
||||
if (dialog == null) { |
||||
return; |
||||
} |
||||
showWindow(activity, dialog.getWindow(), new IWindowShower() { |
||||
@Override |
||||
public void show(Window window) { |
||||
dialog.show(); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
/** |
||||
* 显示窗口【同步窗口系统view的可见度, 解决全屏下显示窗口导致界面退出全屏的问题】 |
||||
* |
||||
* @param activity 活动窗口 |
||||
* @param window 需要显示的窗口 |
||||
* @param iWindowShower 窗口显示接口 |
||||
* @return 是否执行成功 |
||||
*/ |
||||
public static boolean showWindow(Activity activity, Window window, IWindowShower iWindowShower) { |
||||
if (activity == null || window == null || iWindowShower == null) { |
||||
return false; |
||||
} |
||||
window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); |
||||
iWindowShower.show(window); |
||||
StatusBarUtils.syncSystemUiVisibility(activity, window); |
||||
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); |
||||
return true; |
||||
} |
||||
|
||||
/** |
||||
* 同步窗口的系统view的可见度【解决全屏下显示窗口导致界面退出全屏的问题】 |
||||
* |
||||
* @param original 活动窗口 |
||||
* @param target 目标窗口 |
||||
* @return 是否执行成功 |
||||
*/ |
||||
public static boolean syncSystemUiVisibility(Activity original, Window target) { |
||||
if (original == null) { |
||||
return false; |
||||
} |
||||
return syncSystemUiVisibility(original.getWindow(), target); |
||||
} |
||||
|
||||
/** |
||||
* 同步两个窗口的系统view的可见度【解决全屏下显示窗口导致界面退出全屏的问题】 |
||||
* |
||||
* @param original 原始窗口 |
||||
* @param target 目标窗口 |
||||
* @return 是否执行成功 |
||||
*/ |
||||
public static boolean syncSystemUiVisibility(Window original, Window target) { |
||||
if (original == null || target == null) { |
||||
return false; |
||||
} |
||||
target.getDecorView().setSystemUiVisibility(original.getDecorView().getSystemUiVisibility()); |
||||
return true; |
||||
} |
||||
|
||||
/** |
||||
* 窗口显示接口 |
||||
*/ |
||||
public interface IWindowShower { |
||||
/** |
||||
* 显示窗口 |
||||
* |
||||
* @param window 窗口 |
||||
*/ |
||||
void show(Window window); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue