Android-沉浸式的實(shí)現(xiàn)

1、在res中創(chuàng)建目錄values-v21并創(chuàng)建styles.xml文件


GB@}(0}A~5E23U0P5H1B}8I.png

2铅乡、在styles.xml文件中加入控制

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">false</item>
    <!--<item name="android:windowTranslucentNavigation">true</item>-->
    <!--&lt;!&ndash;Android 5.x開始需要把顏色設(shè)置透明榔幸,否則導(dǎo)航欄會(huì)呈現(xiàn)系統(tǒng)默認(rèn)的淺灰色&ndash;&gt;-->
    <!--&lt;!&ndash; 可以修改狀態(tài)欄的顏色 -->
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:navigationBarColor">@android:color/white</item>
    <item name="android:fitsSystemWindows">true</item>
    <!--允許activity的transition轉(zhuǎn)場(chǎng)動(dòng)畫-->
    <item name="android:windowActivityTransitions">true</item>
  </style>
</resources>

3坊谁、創(chuàng)建BaseActivity

public class BaseActivity extends FragmentActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.statusBarLightMode(this);
    }
}

4搔耕、創(chuàng)建StatusBarUtil


import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author renzhiqiang
 * @date 2019/4/24
 */
public class StatusBarUtil {
  /**
   * 修改狀態(tài)欄為全透明
   */
  public static void transparencyBar(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      Window window = activity.getWindow();
      window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      window.getDecorView()
          .setSystemUiVisibility(
              View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
      window.setStatusBarColor(Color.TRANSPARENT);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      Window window = activity.getWindow();
      window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
          WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
  }

  /**
   * 狀態(tài)欄亮色模式隙袁,設(shè)置狀態(tài)欄黑色文字、圖標(biāo)弃榨,
   * 適配4.4以上版本MIUIV菩收、Flyme和6.0以上版本其他Android
   *
   * @return 1:MIUUI 2:Flyme 3:android6.0
   */
  public static void statusBarLightMode(Activity activity) {
    int result = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      if (miuiSetStatusBarLightMode(activity, true)) {
        result = 1;
      } else if (flymeSetStatusBarLightMode(activity.getWindow(), true)) {
        result = 2;
      } else 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);
        result = 3;
      }
    }
  }

  /**
   * 已知系統(tǒng)類型時(shí),設(shè)置狀態(tài)欄黑色文字鲸睛、圖標(biāo)坛梁。
   * 適配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
   *
   * @param type 1:MIUUI 2:Flyme 3:android6.0
   */
  public static void statusBarLightMode(Activity activity, int type) {
    if (type == 1) {
      miuiSetStatusBarLightMode(activity, true);
    } else if (type == 2) {
      flymeSetStatusBarLightMode(activity.getWindow(), true);
    } else if (type == 3) {
      activity.getWindow()
          .getDecorView()
          .setSystemUiVisibility(
              View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }
  }

  /**
   * 狀態(tài)欄暗色模式腊凶,清除MIUI、flyme或6.0以上版本狀態(tài)欄黑色文字、圖標(biāo)
   */
  public static void StatusBarDarkMode(Activity activity, int type) {
    if (type == 1) {
      miuiSetStatusBarLightMode(activity, false);
    } else if (type == 2) {
      flymeSetStatusBarLightMode(activity.getWindow(), false);
    } else if (type == 3) {
      activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
    }
  }

  /**
   * 設(shè)置狀態(tài)欄圖標(biāo)為深色和魅族特定的文字風(fēng)格
   * 可以用來判斷是否為Flyme用戶
   *
   * @param window 需要設(shè)置的窗口
   * @param dark 是否把狀態(tài)欄文字及圖標(biāo)顏色設(shè)置為深色
   * @return boolean 成功執(zhí)行返回true
   */
  public static boolean flymeSetStatusBarLightMode(Window window, boolean dark) {
    boolean result = false;
    if (window != null) {
      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 (dark) {
          value |= bit;
        } else {
          value &= ~bit;
        }
        meizuFlags.setInt(lp, value);
        window.setAttributes(lp);
        result = true;
      } catch (Exception e) {

      }
    }
    return result;
  }

  /**
   * 需要MIUIV6以上
   *
   * @param dark 是否把狀態(tài)欄文字及圖標(biāo)顏色設(shè)置為深色
   * @return boolean 成功執(zhí)行返回true
   */
  public static boolean miuiSetStatusBarLightMode(Activity activity, boolean dark) {
    boolean result = false;
    Window window = activity.getWindow();
    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);
        //noinspection unchecked
        Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
        if (dark) {
          //狀態(tài)欄透明且黑色字體
          extraFlagField.invoke(window, darkModeFlag, darkModeFlag);
        } else {
          //清除黑色字體
          extraFlagField.invoke(window, 0, darkModeFlag);
        }
        result = true;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
          //開發(fā)版 7.7.13 及以后版本采用了系統(tǒng)API钧萍,舊方法無效但不會(huì)報(bào)錯(cuò)褐缠,所以兩個(gè)方式都要加上
          if (dark) {
            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_VISIBLE);
          }
        }
      } catch (Exception e) {

      }
    }
    return result;
  }

  //獲取屏幕虛擬鍵高度
  public static int getVirtualBarHeight(Context context) {
    int vh = 0;
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    DisplayMetrics dm = new DisplayMetrics();
    try {
      @SuppressWarnings("rawtypes") Class c = Class.forName("android.view.Display");
      @SuppressWarnings("unchecked") Method method =
          c.getMethod("getRealMetrics", DisplayMetrics.class);
      method.invoke(display, dm);
      vh = dm.heightPixels - display.getHeight();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return vh;
  }

  public static int getStatusBarHeight(Context context) {
    int height = 0;
    try {
      Class c = Class.forName("com.android.internal.R$dimen");
      Field f = c.getField("status_bar_height");
      int id = (int) f.get(null);
      height = context.getResources().getDimensionPixelSize(id);
    } catch (Exception e) {
    }
    return height;
  }

  public static int dip2px(Context context, float dpValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
  }

  public static int px2dip(Context context, float pxValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (pxValue / scale + 0.5f);
  }

  /** 獲取手機(jī)的密度 */
  public static float getDensity(Context context) {
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    return dm.density;
  }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市风瘦,隨后出現(xiàn)的幾起案子队魏,更是在濱河造成了極大的恐慌,老刑警劉巖万搔,帶你破解...
    沈念sama閱讀 221,576評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件胡桨,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡瞬雹,警方通過查閱死者的電腦和手機(jī)昧谊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來酗捌,“玉大人呢诬,你說我怎么就攤上這事∨昼停” “怎么了尚镰?”我有些...
    開封第一講書人閱讀 168,017評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)哪廓。 經(jīng)常有香客問我狗唉,道長(zhǎng),這世上最難降的妖魔是什么涡真? 我笑而不...
    開封第一講書人閱讀 59,626評(píng)論 1 296
  • 正文 為了忘掉前任分俯,我火速辦了婚禮,結(jié)果婚禮上综膀,老公的妹妹穿的比我還像新娘澳迫。我一直安慰自己夺英,他們只是感情好扯罐,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,625評(píng)論 6 397
  • 文/花漫 我一把揭開白布拢锹。 她就那樣靜靜地躺著萄喳,像睡著了一般卒稳。 火紅的嫁衣襯著肌膚如雪减江。 梳的紋絲不亂的頭發(fā)上辈灼,一...
    開封第一講書人閱讀 52,255評(píng)論 1 308
  • 那天甜紫,我揣著相機(jī)與錄音腰根,去河邊找鬼唠雕。 笑死,一個(gè)胖子當(dāng)著我的面吹牛捕儒,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播点弯,決...
    沈念sama閱讀 40,825評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼碳柱,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼福稳!你這毒婦竟也來了的圆?” 一聲冷哼從身側(cè)響起毁枯,我...
    開封第一講書人閱讀 39,729評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎挠蛉,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體掰担,經(jīng)...
    沈念sama閱讀 46,271評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡勺疼,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,363評(píng)論 3 340
  • 正文 我和宋清朗相戀三年轨淌,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,498評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡谷炸,死狀恐怖禀挫,靈堂內(nèi)的尸體忽然破棺而出旬陡,到底是詐尸還是另有隱情,我是刑警寧澤语婴,帶...
    沈念sama閱讀 36,183評(píng)論 5 350
  • 正文 年R本政府宣布砰左,位于F島的核電站廉羔,受9級(jí)特大地震影響髓削,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,867評(píng)論 3 333
  • 文/蒙蒙 一坎怪、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧廓握,春花似錦搅窿、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春耐朴,著一層夾襖步出監(jiān)牢的瞬間借卧,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工筛峭, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留铐刘,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,906評(píng)論 3 376
  • 正文 我出身青樓影晓,卻偏偏與公主長(zhǎng)得像镰吵,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子挂签,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,507評(píng)論 2 359