Managing System UI
標簽: UI SystemBar
調暗系統Bars
Android4.0(API level 14)及以上使用
此方法不會導致內容大小的變化,它只是簡單的將系統Bars(狀態欄跟導航欄)模糊處理,一旦用戶點擊屏幕,將會恢復正常。目的是為了讓用戶更加專注於APP的內容中。
當點擊導航欄、導航欄上滑、狀態欄下滑、彈出菜單等操作時,標志被清除。
使用:
View decorView = getActivity().getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE;
decorView.setSystemUiVisibility(uiOptions);
恢復也很簡單,只需將uiOptions置為0
View decorView = getActivity().getWindow().getDecorView();
// Calling setSystemUiVisibility() with a value of 0 clears
// all flags.
decorView.setSystemUiVisibility(0);
此方式會引起 onSystemUiVisibilityChange()的回調
隱藏狀態欄
-
Android 4.0及以下
- Android4.0及以下通過設置WindowManager標志完成,具體可以通過清單中設置FullScreen主題,比如:
<application ... android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" > ...
```
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
//注意:通過此方式設置的標志需要手動清除,否則會一直作用,並且onSystemUiVisibilityChange()不會被回調
```
>通過清單配置的好處是UI過渡流暢,因為系統在Activity實例化之前就已經獲取到渲染UI的信息。
- Android 4.1及以上
Android4.1及以上隱藏狀態欄是通過調用
setSystemUiVisibility()
方法 ,需要注意的是,隱藏狀態欄的時候需要把ToolBar也隱藏了
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();
注意:通過此方式隱藏狀態欄,當按下home鍵或者彈出對話框等,標志會被清除,
onSystemUiVisibilityChange()
會被調用,點擊屏幕不會清除標志
隱藏導航欄
Android4.0(API level 14)
通常來說,在隱藏導航欄的時候,需要一並將狀態欄隱藏。
View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
注意:此時點擊屏幕、菜單、home鍵、顯示Dialog等操作,標記即被清,
onSystemUiVisibilityChange()
會被調用
沉浸模式
Android4.4(API level 19)
引入SYSTEM_UI_FLAG_IMMERSIVE
,這個標志可以讓我們真正實現全屏模式,為什么這么說呢?因為前面隱藏導航欄跟狀態欄時,點擊屏幕后標志就會被清除,而在這三者配合使用后,可以使我們捕獲屏幕所有的觸碰事件,此時點擊屏幕並不會清除標志,除非我們從狀態欄下滑,或者從導航欄上滑,這才會清除標志,並且會觸發onSystemUiVisibilityChange()
回調。
另外,如果我們需要在標志被清除后可以自動再次全屏,將SYSTEM_UI_FLAG_IMMERSIVE
替換為SYSTEM_UI_FLAG_IMMERSIVE_STICKY
即可,但是需要注意的是,由於使用此標志之后,系統Bars只是在半透明狀態下短暫地顯示,所以並不會觸發onSystemUiVisibilityChange()
回調。
用法:
// This snippet hides the system bars.
private void hideSystemUI() {
// Set the IMMERSIVE flag.
// Set the content to appear under the system bars so that the content
// doesn't resize when the system bars hide and show.
mDecorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
// This snippet shows the system bars. It does this by removing all the flags
// except for the ones that make the content appear under the system bars.
private void showSystemUI() {
mDecorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
添加回調
通過給View注冊
View.OnSystemUiVisibilityChangeListener
來響應UI變化
前面多次提及的onSystemUiVisibilityChange()
便是View.OnSystemUiVisibilityChangeListener
接口中的方法
View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
// Note that system bars will only be "visible" if none of the
// LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
// TODO: The system bars are visible. Make any desired
// adjustments to your UI, such as showing the action bar or
// other navigational controls.
} else {
// TODO: The system bars are NOT visible. Make any desired
// adjustments to your UI, such as hiding the action bar or
// other navigational controls.
}
}
});