在日常開發(fā)中,我們經(jīng)常會(huì)遇到viewpager配合fragment的情況;這些fragment有的要求頂進(jìn)狀態(tài)欄,就像這樣
也可能會(huì)遇到頂部是標(biāo)題欄,不允許頂進(jìn)狀態(tài)欄;在每個(gè)fragment中處理狀態(tài)欄會(huì)出現(xiàn)這樣的情況
即使設(shè)置了android:fitsSystemWindows="true"? android:clipToPadding="true"這兩個(gè)屬性;在快速切換的時(shí)候吹出現(xiàn)短暫上圖的情況,這里想到一種折衷的方案:就是activity整體是透明的,在每個(gè)fragment頂部寫一個(gè)view動(dòng)態(tài)設(shè)置view的高度來(lái)達(dá)到效果,下面直接貼代碼:
先貼工具類? 這個(gè)工具類可以快速設(shè)置狀態(tài)欄顏色和透明狀態(tài)欄
package com.example.mylibrary.utils;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.view.ViewCompat;
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 java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 透明狀態(tài)欄
*
* 使用透明狀態(tài)欄得時(shí)候 如果有標(biāo)題 需設(shè)置top屬性 高度為22dp
*/
public class StatusBarCompat {
public static int navigationHeight =0;
? ? private static final int COLOR_TRANSLUCENT = Color.parseColor("#00000000");
? ? public static final int DEFAULT_COLOR_ALPHA =112;
? ? /**
* set statusBarColor
? ? * @param statusColor color
? ? * @param alpha 0 - 255
*/
? ? public static void setStatusBarColor(Activity activity, int statusColor, int alpha) {
setStatusBarColor(activity, calculateStatusBarColor(statusColor, alpha));
? ? }
public static void setStatusBarColor(Activity activity, int statusColor) {
Window window = activity.getWindow();
? ? ? ? ViewGroup mContentView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);
? ? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//First translucent status bar.
? ? ? ? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
? ? ? ? ? ? ? ? //After LOLLIPOP not translucent status bar
? ? ? ? ? ? ? ? window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
? ? ? ? ? ? ? ? //Then call setStatusBarColor.
? ? ? ? ? ? ? ? window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
? ? ? ? ? ? ? ? window.setStatusBarColor(statusColor);
? ? ? ? ? ? ? ? //set child View not fill the system window
? ? ? ? ? ? ? ? View mChildView = mContentView.getChildAt(0);
? ? ? ? ? ? ? ? if (mChildView !=null) {
ViewCompat.setFitsSystemWindows(mChildView, true);
? ? ? ? ? ? ? ? }
}else {
ViewGroup mDecorView = (ViewGroup) window.getDecorView();
? ? ? ? ? ? ? ? if (mDecorView.getTag() !=null && mDecorView.getTag()instanceof Boolean && (Boolean)mDecorView.getTag()) {
//if has add fake status bar view
? ? ? ? ? ? ? ? ? ? window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
? ? ? ? ? ? ? ? ? ? View mStatusBarView = mDecorView.getChildAt(0);
? ? ? ? ? ? ? ? ? ? if (mStatusBarView !=null) {
mStatusBarView.setBackgroundColor(statusColor);
? ? ? ? ? ? ? ? ? ? }
}else {
int statusBarHeight =getStatusBarHeight(activity);
? ? ? ? ? ? ? ? ? ? //add margin
? ? ? ? ? ? ? ? ? ? View mContentChild = mContentView.getChildAt(0);
? ? ? ? ? ? ? ? ? ? if (mContentChild !=null) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
? ? ? ? ? ? ? ? ? ? ? ? ViewCompat.setFitsSystemWindows(mContentChild, false);
? ? ? ? ? ? ? ? ? ? ? ? FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mContentChild.getLayoutParams();
? ? ? ? ? ? ? ? ? ? ? ? lp.topMargin += statusBarHeight;
? ? ? ? ? ? ? ? ? ? ? ? mContentChild.setLayoutParams(lp);
? ? ? ? ? ? ? ? ? ? ? ? //add fake status bar view
? ? ? ? ? ? ? ? ? ? ? ? View mStatusBarView =new View(activity);
? ? ? ? ? ? ? ? ? ? ? ? FrameLayout.LayoutParams layoutParams =new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
? ? ? ? ? ? ? ? ? ? ? ? layoutParams.gravity = Gravity.TOP;
? ? ? ? ? ? ? ? ? ? ? ? mStatusBarView.setLayoutParams(layoutParams);
? ? ? ? ? ? ? ? ? ? ? ? mStatusBarView.setBackgroundColor(statusColor);
? ? ? ? ? ? ? ? ? ? ? ? mDecorView.addView(mStatusBarView, 0);
? ? ? ? ? ? ? ? ? ? ? ? mDecorView.setTag(true);
? ? ? ? ? ? ? ? ? ? }
}
}
}
}
public static void translucentStatusBar(Activity activity) {
translucentStatusBar(activity, false);
? ? }
/**
* change to full screen mode
? ? * @param hideStatusBarBackground hide status bar alpha Background when SDK > 21, true if hide it
*/
? ? public static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
Window window = activity.getWindow();
? ? ? ? ViewGroup mContentView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);
? ? ? ? //set child View not fill the system window
? ? ? ? View mChildView = mContentView.getChildAt(0);
? ? ? ? if (mChildView !=null) {
ViewCompat.setFitsSystemWindows(mChildView, false);
? ? ? ? }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int statusBarHeight =getStatusBarHeight(activity);
? ? ? ? ? ? //First translucent status bar.
? ? ? ? ? ? window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
? ? ? ? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//After LOLLIPOP just set LayoutParams.
? ? ? ? ? ? ? ? window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
? ? ? ? ? ? ? ? if (hideStatusBarBackground) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
? ? ? ? ? ? ? ? ? ? window.setStatusBarColor(COLOR_TRANSLUCENT);
? ? ? ? ? ? ? ? }else {
window.setStatusBarColor(calculateStatusBarColor(COLOR_TRANSLUCENT, DEFAULT_COLOR_ALPHA));
? ? ? ? ? ? ? ? }
//must call requestApplyInsets, otherwise it will have space in screen bottom
? ? ? ? ? ? ? ? if (mChildView !=null) {
ViewCompat.requestApplyInsets(mChildView);
? ? ? ? ? ? ? ? }
}else {
ViewGroup mDecorView = (ViewGroup) window.getDecorView();
? ? ? ? ? ? ? ? if (mDecorView.getTag() !=null && mDecorView.getTag()instanceof Boolean && (Boolean)mDecorView.getTag()) {
mChildView = mDecorView.getChildAt(0);
? ? ? ? ? ? ? ? ? ? //remove fake status bar view.
? ? ? ? ? ? ? ? ? ? mContentView.removeView(mChildView);
? ? ? ? ? ? ? ? ? ? mChildView = mContentView.getChildAt(0);
? ? ? ? ? ? ? ? ? ? if (mChildView !=null) {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
? ? ? ? ? ? ? ? ? ? ? ? //cancel the margin top
? ? ? ? ? ? ? ? ? ? ? ? if (lp !=null && lp.topMargin >= statusBarHeight) {
lp.topMargin -= statusBarHeight;
? ? ? ? ? ? ? ? ? ? ? ? ? ? mChildView.setLayoutParams(lp);
? ? ? ? ? ? ? ? ? ? ? ? }
}
mDecorView.setTag(false);
? ? ? ? ? ? ? ? }
}
}
}
//Get status bar height
? ? public static int getStatusBarHeight(Context context) {
int result =0;
? ? ? ? int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
? ? ? ? if (resId >0) {
result = context.getResources().getDimensionPixelOffset(resId);
? ? ? ? }
return result;
? ? }
//Get alpha color
? ? private static int calculateStatusBarColor(int color, int alpha) {
float a =1 - alpha /255f;
? ? ? ? int red = color >>16 &0xff;
? ? ? ? int green = color >>8 &0xff;
? ? ? ? int blue = color &0xff;
? ? ? ? red = (int) (red * a +0.5);
? ? ? ? green = (int) (green * a +0.5);
? ? ? ? blue = (int) (blue * a +0.5);
? ? ? ? return 0xff <<24 | red <<16 | green <<8 | blue;
? ? }
/**
* 改變魅族的狀態(tài)欄字體為黑色,要求FlyMe4以上
*/
? ? private static void processFlyMe(boolean isLightStatusBar, Activity activity) {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
? ? ? ? try {
Class instance = Class.forName("android.view.WindowManager$LayoutParams");
? ? ? ? ? ? int value = instance.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON").getInt(lp);
? ? ? ? ? ? Field field = instance.getDeclaredField("meizuFlags");
? ? ? ? ? ? field.setAccessible(true);
? ? ? ? ? ? int origin = field.getInt(lp);
? ? ? ? ? ? if (isLightStatusBar) {
field.set(lp, origin | value);
? ? ? ? ? ? }else {
field.set(lp, (~value) & origin);
? ? ? ? ? ? }
}catch (Exception ignored) {
ignored.printStackTrace();
? ? ? ? }
}
/**
* 改變小米的狀態(tài)欄字體顏色為黑色, 要求MIUI6以上? lightStatusBar為真時(shí)表示黑色字體
*/
? ? private static void processMIUI(boolean lightStatusBar, Activity activity) {
Class clazz = activity.getWindow().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);
? ? ? ? ? ? extraFlagField.invoke(activity.getWindow(), lightStatusBar? darkModeFlag :0, darkModeFlag);
? ? ? ? }catch (Exception ignored) {
ignored.printStackTrace();
? ? ? ? }
}
private static final StringKEY_MIUI_VERSION_CODE ="ro.miui.ui.version.code";
? ? private static final StringKEY_MIUI_VERSION_NAME ="ro.miui.ui.version.name";
? ? private static final StringKEY_MIUI_INTERNAL_STORAGE ="ro.miui.internal.storage";
? ? /**
* 判斷手機(jī)是否是小米
? ? * @return
? ? */
? ? public static boolean isMIUI() {
try {
final BuildProperties prop = BuildProperties.newInstance();
? ? ? ? ? ? return prop.getProperty(KEY_MIUI_VERSION_CODE, null) !=null
? ? ? ? ? ? ? ? ? ? || prop.getProperty(KEY_MIUI_VERSION_NAME, null) !=null
? ? ? ? ? ? ? ? ? ? || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) !=null;
? ? ? ? }catch (final IOException e) {
return false;
? ? ? ? }
}
/**
* 判斷手機(jī)是否是魅族
? ? * @return
? ? */
? ? public static boolean isFlyme() {
try {
// Invoke Build.hasSmartBar()
? ? ? ? ? ? final Method method = Build.class.getMethod("hasSmartBar");
? ? ? ? ? ? return method !=null;
? ? ? ? }catch (final Exception e) {
return false;
? ? ? ? }
}
/**
* 設(shè)置狀態(tài)欄文字色值為深色調(diào)
? ? * @param useDart 是否使用深色調(diào)
? ? * @param activity
? ? */
? ? public static void setStatusTextColor(boolean useDart, Activity activity) {
//? ? ? ? if (isFlyme()) {
//? ? ? ? ? ? processFlyMe(useDart, activity);
//? ? ? ? } else if (isMIUI()) {
//? ? ? ? ? ? processMIUI(useDart, activity);
//? ? ? ? } else {
? ? ? ? ? ? if (useDart) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
? ? ? ? ? ? ? ? }
}else {
activity.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
? ? ? ? ? ? }
activity.getWindow().getDecorView().findViewById(android.R.id.content).setPadding(0, 0, 0, StatusBarCompat.navigationHeight);
? ? ? ? }
//? ? }
}
在狀態(tài)欄不為透明的時(shí)候;動(dòng)態(tài)設(shè)置view的高度為狀態(tài)欄的高度,來(lái)頂進(jìn)狀態(tài)欄同時(shí)可以設(shè)置view的顏色,以此來(lái)代替狀態(tài)欄的顏色;在狀態(tài)欄為透明的時(shí)候view的高度默認(rèn)為0,即圖片可以直接伸進(jìn)狀態(tài)欄里面;
View view = aa.findViewById(R.id.view);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
view.setBackgroundColor(Color.YELLOW);
layoutParams.height = StatusBarCompat.getStatusBarHeight(getActivity()); //這里拿到狀態(tài)欄的高度
Log.i(TAG, "onCreateView: "+StatusBarCompat.getStatusBarHeight(getActivity()));
view.setLayoutParams(layoutParams);//設(shè)置view高度