欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Android沉浸式實(shí)現(xiàn)兼容解決辦法

 更新時(shí)間:2017年11月15日 09:42:12   作者:Cosecant  
本篇文章主要介紹了詳解Android沉浸式實(shí)現(xiàn)兼容解決辦法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

自android5.0開始,沉浸式狀態(tài)欄似乎成為一種潮流,應(yīng)用里缺少沉浸式總感覺少些什么。于是乎,我開始到處找如何兼容低版本的沉浸式,由于Android平臺(tái)跨度問題,總遇到一些不如人意的問題。終于,皇天不負(fù)有心人,通過參考一些網(wǎng)絡(luò)上的資料以及開發(fā)的一些經(jīng)驗(yàn),總結(jié)出一個(gè)可行的且良好的解決方案!

先介紹下,什么是沉浸式狀態(tài)欄?

沉浸式,要求在應(yīng)用中Android狀態(tài)欄(StatusBar)與標(biāo)題欄(ActionBar/Toolbar)要擁有相同的顏色,或者使用同一張圖的連續(xù)背景。


話不多說,亮劍吧!

具體實(shí)現(xiàn)需要針對(duì)不同Android版本做處理,還有針對(duì)DecorView做處理以及做activity的xml布局文件根布局控件做屬性處理。

java代碼,設(shè)置沉浸式的方法

  /**
   * 設(shè)置沉浸式狀態(tài)欄顏色
   *
   * @param colorResId 狀態(tài)欄顏色
   */
  protected void setImmersiveStatusBarColor(@ColorRes int colorResId) {
    int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      int statusBarColor = ApkUtil.getColor(this, colorResId); //①
      float lightDegress = (Color.red(statusBarColor) + Color.green(statusBarColor) + Color.blue(statusBarColor)) / 3; //作色彩亮度判斷,好針對(duì)顏色做相應(yīng)的狀態(tài)欄的暗色還是亮色。
      if ((lightDegress > 200 || lightDegress == 0) && Build.VERSION.SDK_INT > Build.VERSION_CODES.M)
        rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
      window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      window.setStatusBarColor(statusBarColor);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      rootView.setSystemUiVisibility(flags | View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      rootView.setSystemUiVisibility(flags);
    }
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { //當(dāng)API小于等于19,此時(shí)為了實(shí)現(xiàn)沉浸式狀態(tài)欄,需要添加一個(gè)view來做statusbar背景控件
      final boolean isHasStatusBarView = rootView.getTag() != null;
      View statusbarView = !isHasStatusBarView ? new View(this) : (View)rootView.getTag();
      statusbarView.setBackgroundResource(colorResId);
      if(!isHasStatusBarView) {
        rootView.setTag(statusBarView);
        statusbarView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, ViewUtil.getStatusBarHeight(this))); //②
        rootView.addView(statusbarView);
      }
    }
  }

注:此處針對(duì)rootView(即DecorView)、window的獲取不再陳述!

①.ApkUtil.getColor(this, colorResId)

  /**
   * 獲取顏色資源
   * @param context 上下文對(duì)象
   * @param colorId 顏色ResId
   * @return
   */
  @SuppressWarnings("deprecation")
  public static int getColor(Context context, int colorId) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      return context.getColor(colorId);
    }
    return context.getResources().getColor(colorId);
  }

②. 獲取狀態(tài)欄高度

  /**
   * 獲取狀態(tài)欄高度
   * @param context 上下文對(duì)象
   */
  @JvmStatic
  @SuppressLint("PrivateApi")
  fun getStatusBarHeight(context: Context): Int {
    val clazz = Class.forName("com.android.internal.R\$dimen")
    val obj = clazz?.newInstance()
    val field = clazz.getField("status_bar_height")
    field?.let {
      field.isAccessible = true
      val x = Integer.parseInt(field.get(obj).toString())
      return context.resources.getDimensionPixelSize(x)
    }
    return 75
  }

activity布局xml根布局添加以下屬性

 android:fitsSystemWindows="true"
 android:clipToPadding="false"

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論