沉浸式狀態欄

 Android 5.0(API 21)以上實現沉浸式的方式android

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//注意要清除 FLAG_TRANSLUCENT_STATUS flag
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().setStatusBarColor(getResources().getColor(android.R.color.holo_red_light)

固然也能夠直接在Theme中使用,在values-v21文件夾下添加以下主題:佈局

<style name="ImageTranslucentTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:windowTranslucentStatus">true</item>
    <!-- 設置statusBarColor 爲透明-->
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

代碼中經過版本號的判斷兼容 Android5.0如下和Android 5.0以上:ui

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
      activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      activity.getWindow().setStatusBarColor(calculateStatusColor(color, statusBarAlpha));
  } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
      int count = decorView.getChildCount();
      if (count > 0 && decorView.getChildAt(count - 1) instanceof StatusBarView) {
          decorView.getChildAt(count - 1).setBackgroundColor(calculateStatusColor(color, statusBarAlpha));
      } else {
          StatusBarView statusView = createStatusBarView(activity, color, statusBarAlpha);
          decorView.addView(statusView);
      }

      ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
      rootView.setFitsSystemWindows(true);
      rootView.setClipToPadding(true);
      setRootView(activity);
  }

 

Android 6.0 + 實現狀態欄字色和圖標淺黑色spa

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
       getWindow().getDecorView().setSystemUiVisibility(
               View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
   }

除了在代碼中添加之外,還能夠直接在主題中使用屬性:code

<style name="MDTheme" parent="Theme.Design.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">false</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/holo_red_light</item>
        <!-- Android 6.0以上 狀態欄字色和圖標爲淺黑色-->
        <item name="android:windowLightStatusBar">true</item>
    </style>

Android4.4(API 19) - Android 5.0(API 21)實現沉浸式的方式視頻

有兩種方式實現這個屬性:圖片

能夠在代碼中設置,以下:ip

activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

固然也能夠在theme 中設置屬性windowTranslucentStatus,以下:get

android:windowTranslucentStatus

效果以下:it

 

效果如上圖,能夠看出,沉浸式的效果是出來了,可是也有一個問題,咱們的標題欄和狀態欄重疊了,至關於整個佈局上移了StatusBar 的高度。

爲了讓標題欄回到原來的位置,咱們在標題欄的上方添加一個大小和StatusBar大小同樣的View,View 的BackgroundColor 爲標題欄同樣的顏色,這個View起到一個佔位的做用。這個時候,標題欄就會下移StatusBar的高度,回到正常的位置。

添加以下代碼:

//獲取windowphone下的decorView
        ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
        int       count     = decorView.getChildCount();
        //判斷是否已經添加了statusBarView
        if (count > 0 && decorView.getChildAt(count - 1) instanceof StatusBarView) {
            decorView.getChildAt(count - 1).setBackgroundColor(calculateStatusColor(color, statusBarAlpha));
        } else {
            //新建一個和狀態欄高寬的view
            StatusBarView statusView = createStatusBarView(activity, color, statusBarAlpha);
            decorView.addView(statusView);
        }
        ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
        //rootview不會爲狀態欄留出狀態欄空間
        ViewCompat.setFitsSystemWindows(rootView,true);
        rootView.setClipToPadding(true);

建立和status bar 同樣大小的View的代碼以下:

private static StatusBarView createStatusBarView(Activity activity, int color, int alpha) {
        // 繪製一個和狀態欄同樣高的矩形
        StatusBarView statusBarView = new StatusBarView(activity);
        LinearLayout.LayoutParams params =
                new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
        statusBarView.setLayoutParams(params);
        statusBarView.setBackgroundColor(calculateStatusColor(color, alpha));
        return statusBarView;
    }

其中StatusBarView 就是一個普通的View。

添加上述代碼後,效果以下:

 

 

經過以上就能夠實現Android 4.4 上的沉浸式狀態欄。

另外,若是是一張圖片延伸到狀態欄的話,直接設置FLAG_TRANSLUCENT_STATUS就能夠了,

補充:

若是設置添加標題欄位置的話,視頻全屏切換的時候會留出一個view的位子。不行仍是在styles中設置

//配合沉浸式狀態欄使用,防止標題欄置頂
<item name="android:fitsSystemWindows">true</item>

這樣會致使Toast文字錯位

解決方法以下:使用應用級別的上下文

Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();

完美解決!

相關文章
相關標籤/搜索