沉浸式體驗
首先別吐槽沉浸式
這個名詞吧,畢竟這各名字是廣為人知並且比透明狀態列加透明導航欄
更酷。充分使用整個螢幕將這2個系統檢視融入自己APP也算沉浸式體驗吧。
首先2個名詞:
StatusBar:
NavigationBar:
下面是Google的官方標準模版:
在官方示例中:
StatusBar是一個半透明陰影,View可以伸展到其後面。
NavigationBar是純黑不能使用的。
Google提供NavigationBar的透明與使用的可能,卻沒有推薦使用。個人覺得是為了給
Bottom navigation
做準備。這裡不討論Bottom navigation的優劣(我是Bottom navigation黑)。
下面是B站的:
不可否認使用NavigationBar空間對多下巴手機是種拯救。
(Google自己推的虛擬按鍵,卻自己放棄了屏佔比這一大優勢。)
好了,B站的UI就是我所期望的UI。下面我們就來實現它。
style的配置
android從4.4開始,開始支援UI使用StatusBar與NavigationBar的範圍。
所以要進行下面的配置:
在value中的styles.xml中設定
1 2 3 4 5 |
<!-- Base application theme. --> <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> </style> <style name="AppTheme" parent="AppTheme.Base"></style> |
在value-v19中的styles.xml中設定(為了相容4.4)
1 2 3 4 |
<style name="AppTheme" parent="AppTheme.Base"> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">true</item> </style> |
1 2 3 4 5 6 7 8 |
<style name="AppTheme" parent="AppTheme.Base"> <!--透明狀態列--> <item name="android:windowTranslucentStatus">true</item> <!--透明導航欄--> <item name="android:windowTranslucentNavigation">true</item> <!--使狀態列,導航欄可繪製--> <item name="android:windowDrawsSystemBarBackgrounds">true</item> </style> |
然後使用AppTheme這個主題,這是1個示例,應該看得出來吧。只要在你的AppTheme的v19版本和v21版本新增了相應屬性就好。
使用ToolBar
當你使用了StatusBar的部分,就不要再使用ActionBar了。
現在基本都會使用ToolBar了吧。還不會可以參考 ToolBar的使用
然後效果如下:
然後這裡就有2個問題:
1. Toolbar到了StatusBar的下面(為了凸顯問題給Toolbar設了顏色)
2. View在NavigationBar下難以點選
這2個問題也是理所應當就應該存在的。
解決方法:
FitSystemWindowLayout
這裡就先說結果吧,使用FitSystemWindowLayout,我為此做了很多適配工作。
這是標準介面:
這個庫提供自動適應StatusBar與NavigationBar的幾個Layout。在XML中設定即可。
這是上面標準UI的XML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?xml version="1.0" encoding="utf-8"?> <com.jude.fitsystemwindowlayout.FitSystemWindowsFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:padding_status="false"//不自動騰出StatusBar的空間,為了讓圖片在StatusBar下繪製 tools:context="com.jude.demo.MainActivity"> //這個ViewPager裡面放的ImageView <com.jude.rollviewpager.RollPagerView android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="300dp" app:rollviewpager_play_delay="3000"/> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?actionBarSize" android:background="#0000"//透明ToolBar app:theme="@style/AppTheme.Dark" app:margin_status="true"//讓ToolBar去適應StatusBar /> <ScrollView android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" app:padding_navigation="true"//只對可滑動View有效的屬性,自動增加底部內Padding android:layout_marginTop="300dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/Base.TextAppearance.AppCompat.Display3" android:text="A\nB\nC\nD\nE\nF\nG"/> </ScrollView> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:margin_navigation="true"//讓FAB去適應NavigationBar android:src="@android:drawable/ic_dialog_email" /> </com.jude.fitsystemwindowlayout.FitSystemWindowsFrameLayout> |
FitSystemWindow的原理
Android4.4與Android5.0的Insets處理機制完全不同。
Android 5.0的機制:
ViewRootImpl.java中掌管View繪製的函式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
private void performTraversals() { …… dispatchApplyInsets(host); …… performMeasure(childWidthMeasureSpec, childHeightMeasureSpec); …… performLayout(lp, desiredWindowWidth, desiredWindowHeight); …… performDraw(); } void dispatchApplyInsets(View host) { host.dispatchApplyWindowInsets(getWindowInsets(true /* forceConstruct */)); } |
SystemBar的尺寸在WindowInsets 中表示出來,比如Insets:(0, 63 , 0, 126)
。表示StatusBar高度63,NavigationBar高度126.
dispatchApplyWindowInsets 將WindowInsets 從View樹頂部開始分發。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//View.java 程式碼經過精簡 public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) { if (mListenerInfo != null && mListenerInfo.mOnApplyWindowInsetsListener != null) { return mListenerInfo.mOnApplyWindowInsetsListener.onApplyWindowInsets(this, insets); } else { return onApplyWindowInsets(insets); } } -------------------------------------------------------------------- //ViewGroup.java @Override public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) { insets = super.dispatchApplyWindowInsets(insets); if (!insets.isConsumed()) { final int count = getChildCount(); for (int i = 0; i < count; i++) { insets = getChildAt(i).dispatchApplyWindowInsets(insets); if (insets.isConsumed()) { break; } } } return insets; } |
這個方法很簡單。在View樹中分發Insets,先序遍歷。一旦被消費就終止分發。可以看到處理WindowInsets 主要是2個方式。
自定義Insets處理方式的方法1
給目標View註冊監聽
View接收到Insets。會先判斷自己是否被註冊了監聽,監聽是指這個,在這個監聽裡能夠收到Insets。並依據自己情況處理。
1 2 3 |
public void setOnApplyWindowInsetsListener(OnApplyWindowInsetsListener listener) { getListenerInfo().mOnApplyWindowInsetsListener = listener; } |
自定義Insets處理方式的方法2
重寫onApplyWindowInsets
先看看預設的實現。
1 2 3 4 5 6 7 8 |
//程式碼經過精簡 public WindowInsets onApplyWindowInsets(WindowInsets insets) { if (fitSystemWindowsInt(insets.getSystemWindowInsets())) { //如果fitSystemWindowsInt返回true就消耗Instes,好簡單的邏輯 return insets.consumeSystemWindowInsets(); } return insets; } |
重點來了fitSystemWindowsInt
.它實質性的判斷並設定了Padding。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
private boolean fitSystemWindowsInt(Rect insets) { //如果設定了FITS_SYSTEM_WINDOWS這個flag if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) { mUserPaddingStart = UNDEFINED_PADDING; mUserPaddingEnd = UNDEFINED_PADDING; Rect localInsets = sThreadLocal.get(); if (localInsets == null) { localInsets = new Rect(); sThreadLocal.set(localInsets); } //computeFitSystemWindows主要就是localInsets=insets。並清空insets boolean res = computeFitSystemWindows(insets, localInsets); mUserPaddingLeftInitial = localInsets.left; mUserPaddingRightInitial = localInsets.right; //直接應用這個Insets到padding internalSetPadding(localInsets.left, localInsets.top, localInsets.right, localInsets.bottom); return res; } return false; } |
而FITS_SYSTEM_WINDOWS
這個flag有2個來源:
程式碼手動設定:
1 2 3 |
public void setFitsSystemWindows(boolean fitSystemWindows) { setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS); } |
在XML中設定android:fitSystemWindow="true"
:
1 2 3 4 5 6 |
case com.android.internal.R.styleable.View_fitsSystemWindows: if (a.getBoolean(attr, false)) { viewFlagValues |= FITS_SYSTEM_WINDOWS; viewFlagMasks |= FITS_SYSTEM_WINDOWS; } break; |
這就很明顯了。
設定了fitSystemWindow
,預設就會消費掉Insets,並設定padding。如果沒有設定,會繼續遍歷直到被某個View消耗。
其實上面的程式碼是精簡後的,實際上對4.4的機制做了一些相容處理。為了便於理解刪掉了。
Android 4.4的機制:
4.4的機制比5.0簡單得多。
1 2 3 4 5 6 7 8 9 10 |
private void performTraversals() { …… host.fitSystemWindows(mFitSystemWindowsInsets); …… performMeasure(childWidthMeasureSpec, childHeightMeasureSpec); …… performLayout(lp, desiredWindowWidth, desiredWindowHeight); …… performDraw(); } |
讓DecorView執行fitSystemWindows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
//ViewGroup.java @Override protected boolean fitSystemWindows(Rect insets) { boolean done = super.fitSystemWindows(insets); if (!done) { final int count = mChildrenCount; final View[] children = mChildren; for (int i = 0; i < count; i++) { done = children[i].fitSystemWindows(insets); if (done) { break; } } } return done; } ---------------------------------------------------- //View.java //與5.0的fitSystemWindowsInt方法一樣 protected boolean fitSystemWindows(Rect insets) { if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) { mUserPaddingStart = UNDEFINED_PADDING; mUserPaddingEnd = UNDEFINED_PADDING; Rect localInsets = sThreadLocal.get(); if (localInsets == null) { localInsets = new Rect(); sThreadLocal.set(localInsets); } boolean res = computeFitSystemWindows(insets, localInsets); mUserPaddingLeftInitial = localInsets.left; mUserPaddingRightInitial = localInsets.right; internalSetPadding(localInsets.left, localInsets.top, localInsets.right, localInsets.bottom); return res; } return false; } |
分發的過程變到了這裡。並且直接就應用了。
與5.0有很大不同(簡單了好多)。
重寫fitSystemWindows
方法即可實現與5.0一樣的效果。