移动互联网技术分享

管理系统状态栏和导航栏(翻译)

2017-03-06  本文已影响29人  黑泥卡

原文: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:

方法二:修改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.
        }
    }
});

使用这个监听可以使整个界面同步对整个界面的一致性是很有好处的。

上一篇下一篇

猜你喜欢

热点阅读