package com.benben.commoncore.utils;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import com.readystatesoftware.systembartint.SystemBarTintManager;
/**
* Create by wanghk on 2019-05-28.
* Describe:狀態列utils
*/
public class StatusBarUtils {
//修改狀態列文字顏色
public static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) {
View decor = activity.getWindow().getDecorView();
if (dark) {
decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
}
/**
* 修改狀態列顏色,支援4.4以上版本
* @param activity
* @param colorId
*/
public static void setStatusBarColor(Activity activity, @ColorRes int colorId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(activity.getResources().getColor(colorId));
// window.setNavigationBarColor(activity.getResources().getColor(colorId));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//使用SystemBarTint庫使4.4版本狀態列變色,需要先將狀態列設定為透明
// transparencyBar(activity);
Window window = activity.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
SystemBarTintManager tintManager = new SystemBarTintManager(activity);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(colorId);
}
}
/**
* 設定狀態列漸變顏色
*
* @param activity 目標activity
* @param view 目標View
*/
public static void setGradientColor(@NonNull Activity activity, View view) {
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
View fakeStatusBarView = decorView.findViewById(android.R.id.custom);
if (fakeStatusBarView != null) {
decorView.removeView(fakeStatusBarView);
}
setRootView(activity, false);
setTransparentForWindow(activity);
setPaddingTop(activity, view);
}
/**
* 設定根佈局引數
*
* @param activity 目標activity
* @param fitSystemWindows 是否預留toolbar的高度
*/
private static void setRootView(Activity activity, boolean fitSystemWindows) {
ViewGroup parent = activity.findViewById(android.R.id.content);
for (int i = 0, count = parent.getChildCount(); i < count; i++) {
View childView = parent.getChildAt(i);
if (childView instanceof ViewGroup) {
childView.setFitsSystemWindows(fitSystemWindows);
((ViewGroup) childView).setClipToPadding(fitSystemWindows);
}
}
}
/**
* 設定透明狀態列
*
* @param activity 目標介面
*/
public static void setTransparentForWindow(@NonNull Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
activity.getWindow()
.getDecorView()
.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
activity.getWindow()
.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
/**
* 增加View的paddingTop,增加的值為狀態列高度 (智慧判斷,並設定高度)
*
* @param context 目標Context
* @param view 需要增高的View
*/
public static void setPaddingTop(Context context, @NonNull View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp != null && lp.height > 0 && view.getPaddingTop() == 0) {
lp.height += getStatusBarHeight(context);
view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + getStatusBarHeight(context),
view.getPaddingRight(), view.getPaddingBottom());
// if(view instanceof ViewGroup){
// for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
// View childView = ((ViewGroup) view).getChildAt(i);
// if (childView instanceof TextView) {
// childView.setPadding(0, getStatusBarHeight(context)-DensityUtil.dip2px(context,2),
// 0, 0);
// }
// }
// }
}
}
}
/**
* 獲取狀態列高度
*
* @param context 目標Context
*/
public static int getStatusBarHeight(Context context) {
// 獲得狀態列高度
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
return context.getResources().getDimensionPixelSize(resourceId);
}
}