package cgg.com.threeapp.utils;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
/**
* Created by Administrator on 2018-03-02.
*/
public class ChenjinUtil {
// 重置状态栏。即把状态栏颜色恢复为系统默认的黑色
public static void reset(Activity activity) {
setStatusBarColor(activity, Color.BLACK);
}
// 设置状态栏的背景色。对于Android4.4和Android5.0以上版本要区分处理
public static void setStatusBarColor(Activity activity, int color) {
//开始沉浸
//startChenJin(activity);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//19 4.4之后才支持沉浸式
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0
activity.getWindow().setStatusBarColor(color);
// 底部导航栏颜色也可以由系统设置
//activity.getWindow().setNavigationBarColor(color);
} else {
setKitKatStatusBarColor(activity, color);
}
if (color == Color.TRANSPARENT) { // 透明背景表示要悬浮状态栏
removeMarginTop(activity);
} else { // 其它背景表示要恢复状态栏
addMarginTop(activity);
}
}
}
private static final String TAG_FAKE_STATUS_BAR_VIEW = "statusBarView";
private static final String TAG_MARGIN_ADDED = "marginAdded";
// 添加顶部间隔,留出状态栏的位置
private static void addMarginTop(Activity activity) {
Window window = activity.getWindow();
ViewGroup contentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
View child = contentView.getChildAt(0);
if (!TAG_MARGIN_ADDED.equals(child.getTag())) {
Log.e("--------","-------add--222222222222-");
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) child.getLayoutParams();
// 添加的间隔大小就是状态栏的高度
params.topMargin += getStatusBarHeight(activity);
child.setLayoutParams(params);
child.setTag(TAG_MARGIN_ADDED);
}
}
// 移除顶部间隔,霸占状态栏的位置
private static void removeMarginTop(Activity activity) {
Window window = activity.getWindow();
ViewGroup contentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
View child = contentView.getChildAt(0);
if (TAG_MARGIN_ADDED.equals(child.getTag())) {
Log.e("--------","-------remove--1111111111-");
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) child.getLayoutParams();
// 移除的间隔大小就是状态栏的高度
params.topMargin -= getStatusBarHeight(activity);
child.setLayoutParams(params);
child.setTag(null);
}
}
// 对于Android4.4,系统没有提供设置状态栏颜色的方法,只能手工搞个假冒的状态栏来占坑
private static void setKitKatStatusBarColor(Activity activity, int statusBarColor) {
Window window = activity.getWindow();
ViewGroup decorView = (ViewGroup) window.getDecorView();
// 先移除已有的冒牌状态栏
View fakeView = decorView.findViewWithTag(TAG_FAKE_STATUS_BAR_VIEW);
if (fakeView != null) {
decorView.removeView(fakeView);
}
// 再添加新来的冒牌状态栏
View statusBarView = new View(activity);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
params.gravity = Gravity.TOP;
statusBarView.setLayoutParams(params);
statusBarView.setBackgroundColor(statusBarColor);
statusBarView.setTag(TAG_FAKE_STATUS_BAR_VIEW);
decorView.addView(statusBarView);
}
/**
* 获取状态栏高度的方法
* @param activity
* @return
*/
public static int getStatusBarHeight(Activity activity) {
int result = 0;
int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = activity.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
public static void startChenJin(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Android5.0之后的沉浸式状态栏写法
Window window = activity.getWindow();
View decorView = window.getDecorView();
// 两个标志位要结合使用,表示让应用的主体内容占用系统状态栏的空间
// 第三个标志位可让底部导航栏变透明View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
decorView.setSystemUiVisibility(option);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else {
// Android4.4的沉浸式状态栏写法
Window window = activity.getWindow();
WindowManager.LayoutParams attributes = window.getAttributes();
int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
// 底部导航栏也可以弄成透明的
//int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
attributes.flags |= flagTranslucentStatus;
//attributes.flags |= flagTranslucentNavigation;
window.setAttributes(attributes);
}
}
}
}
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
/**
* Created by Administrator on 2018-03-02.
*/
public class ChenjinUtil {
// 重置状态栏。即把状态栏颜色恢复为系统默认的黑色
public static void reset(Activity activity) {
setStatusBarColor(activity, Color.BLACK);
}
// 设置状态栏的背景色。对于Android4.4和Android5.0以上版本要区分处理
public static void setStatusBarColor(Activity activity, int color) {
//开始沉浸
//startChenJin(activity);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//19 4.4之后才支持沉浸式
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0
activity.getWindow().setStatusBarColor(color);
// 底部导航栏颜色也可以由系统设置
//activity.getWindow().setNavigationBarColor(color);
} else {
setKitKatStatusBarColor(activity, color);
}
if (color == Color.TRANSPARENT) { // 透明背景表示要悬浮状态栏
removeMarginTop(activity);
} else { // 其它背景表示要恢复状态栏
addMarginTop(activity);
}
}
}
private static final String TAG_FAKE_STATUS_BAR_VIEW = "statusBarView";
private static final String TAG_MARGIN_ADDED = "marginAdded";
// 添加顶部间隔,留出状态栏的位置
private static void addMarginTop(Activity activity) {
Window window = activity.getWindow();
ViewGroup contentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
View child = contentView.getChildAt(0);
if (!TAG_MARGIN_ADDED.equals(child.getTag())) {
Log.e("--------","-------add--222222222222-");
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) child.getLayoutParams();
// 添加的间隔大小就是状态栏的高度
params.topMargin += getStatusBarHeight(activity);
child.setLayoutParams(params);
child.setTag(TAG_MARGIN_ADDED);
}
}
// 移除顶部间隔,霸占状态栏的位置
private static void removeMarginTop(Activity activity) {
Window window = activity.getWindow();
ViewGroup contentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
View child = contentView.getChildAt(0);
if (TAG_MARGIN_ADDED.equals(child.getTag())) {
Log.e("--------","-------remove--1111111111-");
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) child.getLayoutParams();
// 移除的间隔大小就是状态栏的高度
params.topMargin -= getStatusBarHeight(activity);
child.setLayoutParams(params);
child.setTag(null);
}
}
// 对于Android4.4,系统没有提供设置状态栏颜色的方法,只能手工搞个假冒的状态栏来占坑
private static void setKitKatStatusBarColor(Activity activity, int statusBarColor) {
Window window = activity.getWindow();
ViewGroup decorView = (ViewGroup) window.getDecorView();
// 先移除已有的冒牌状态栏
View fakeView = decorView.findViewWithTag(TAG_FAKE_STATUS_BAR_VIEW);
if (fakeView != null) {
decorView.removeView(fakeView);
}
// 再添加新来的冒牌状态栏
View statusBarView = new View(activity);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
params.gravity = Gravity.TOP;
statusBarView.setLayoutParams(params);
statusBarView.setBackgroundColor(statusBarColor);
statusBarView.setTag(TAG_FAKE_STATUS_BAR_VIEW);
decorView.addView(statusBarView);
}
/**
* 获取状态栏高度的方法
* @param activity
* @return
*/
public static int getStatusBarHeight(Activity activity) {
int result = 0;
int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = activity.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
public static void startChenJin(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Android5.0之后的沉浸式状态栏写法
Window window = activity.getWindow();
View decorView = window.getDecorView();
// 两个标志位要结合使用,表示让应用的主体内容占用系统状态栏的空间
// 第三个标志位可让底部导航栏变透明View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
decorView.setSystemUiVisibility(option);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else {
// Android4.4的沉浸式状态栏写法
Window window = activity.getWindow();
WindowManager.LayoutParams attributes = window.getAttributes();
int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
// 底部导航栏也可以弄成透明的
//int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
attributes.flags |= flagTranslucentStatus;
//attributes.flags |= flagTranslucentNavigation;
window.setAttributes(attributes);
}
}
}
}