Fragment中設(shè)置透明狀態(tài)欄

在日常開發(fā)中,我們經(jīng)常會(huì)遇到viewpager配合fragment的情況;這些fragment有的要求頂進(jìn)狀態(tài)欄,就像這樣

圖片頂進(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高度

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,882評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,208評(píng)論 3 414
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,746評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,666評(píng)論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,477評(píng)論 6 407
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,960評(píng)論 1 321
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,047評(píng)論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,200評(píng)論 0 288
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,726評(píng)論 1 333
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,617評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,807評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,327評(píng)論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,049評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,425評(píng)論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,674評(píng)論 1 281
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,432評(píng)論 3 390
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,769評(píng)論 2 372

推薦閱讀更多精彩內(nèi)容