管理系統狀態列和導航欄(翻譯)

heinika1476775901521發表於2017-12-14

原文:https://developer.android.com/training/system-ui/index.html

管理系統UI

system bars 是專用於顯示裝置的提醒和訊息的區域。通常情況下它會顯示在螢幕上方,但是使用者在使用視訊,圖片等沉浸式應用時,可以暫時暗淡系統欄使使用者更專心,或者直接隱藏以達到完全的沉浸式體驗。

如果你熟悉android的設計模式,你就知道符合標準設計規範的重要性。你需要根據使用者體驗對狀態列進行適當的修改。

這節課描述了在不同版本中如何暗淡和隱藏狀態列提供沉浸式的體驗,又保持可以容易的調出狀態列。

課程

暗淡系統欄

學習怎樣暗淡狀態列和導航欄。
支援android4.0以上。

暗淡狀態列和導航欄

使用 View.SYSTEM_UI_FLAG_LOW_PROFILE 標記。

// This example uses decor view, but you can use any visible view.
View decorView = getActivity().getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE;
decorView.setSystemUiVisibility(uiOptions);
複製程式碼

當調出導航欄後狀態列就會完全顯示,如果你想要重新暗淡,須要再設定一遍。

完全顯示狀態列和導航欄

View decorView = getActivity().getWindow().getDecorView();
// Calling setSystemUiVisibility() with a value of 0 clears
// all flags.
decorView.setSystemUiVisibility(0);
複製程式碼

隱藏狀態列

4.0及4.0以下

方法一:修改manifest檔案

<application
    ...
    android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
    ...
</application>
複製程式碼

The advantages of using an activity theme are as follows:

  • It's easier to maintain and less error-prone than setting a flag programmatically.
  • It results in smoother UI transitions, because the system has the information it needs to render your UI before instantiating your app's main activity.

方法二:修改WindowManager flags

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // If the Android version is lower than Jellybean, use this call to hide
        // the status bar.
        if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        setContentView(R.layout.activity_main);
    }
    ...
}
複製程式碼

When you set WindowManager flags (whether through an activity theme or programmatically), the flags remain in effect unless your app clears them.

You can use FLAG_LAYOUT_IN_SCREEN to set your activity layout to use the same screen area that's available when you've enabled FLAG_FULLSCREEN. This prevents your content from resizing when the status bar hides and shows.

4.1及以上

使用setSystemUiVisibility() 比修改WindowManager flags 更細化。

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();
複製程式碼

隱藏導航欄

使用全屏沉浸式模式

使用沉浸式全屏模式 Android 4.4 (API Level 19) 加入了一種新的 UI 標記 SYSTEM_UI_FLAG_IMMERSIVE 用來使你的應用進入真正的全屏。當這個標記結合 SYSTEM_UI_FLAG_HIDE_NAVIGATION 和 SYSTEM_UI_FLAG_FULLSCREEN 時,就可以隱藏狀態列,並捕獲所有的 touch 事件。

當沉浸式模式啟動的時候,依然接收全部的 touch 事件。使用者可以通過向內測的滑動來撥出狀態列和導航欄。這個操作會清除 SYSTEM_UI_FLAG_HIDE_NAVIGATION flag (和 SYSTEM_UI_FLAG_FULLSCREEN 如果設定了),所以系統欄可以顯示。這個操作也會觸發 View.OnSystemUiVisivilityChangeListener 。 但是如果你希望系統欄在一段時間後自動隱藏 ,可以使用 SYSTEM_UI_FLAG_IMMERSIVE_STICKY 。注意 “sticky” 不會觸發任何監聽。

沉浸式模式的4種狀態

響應UI檢視可見化的改變

那麼如何響應狀態列的改變呢?如果你想要其他檢視也同步狀態列的隱藏和消失,可以使用 View.OnSystemUiVisibilityChangeListener 。

例子,你可以在oncreate中新增:

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.
        }
    }
});
複製程式碼

使用這個監聽可以使整個介面同步對整個介面的一致性是很有好處的。

相關文章