具有沉浸式全屏模式的工具栏

时间:2015-02-27 13:14:31

标签: android android-actionbar android-toolbar

是否可以将Android KitKat和Lollipop上的Immersive Mode与工具栏结合使用?

工具栏

 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

隐藏操作栏(工具栏)

    // This snippet hides the system bars.
    // https://developer.android.com/training/system-ui/immersive.html
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.

    getWindow().getDecorView().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);

    getSupportActionBar().hide();
}

这很好用。但工具栏不会再出现。

3 个答案:

答案 0 :(得分:3)

  

试试这个

 @Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        if (hasFocus) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }
}

答案 1 :(得分:1)

这可能有助于其他人,我已经解决了使用View.OnSystemUiVisibilityChangeListener这里是代码snipet

--------

 View mDecorView = getWindow().getDecorView();
 mDecorView.setOnSystemUiVisibilityChangeListener(mOnSystemUiVisibilityChangeListener);
-------


hideSystemUI();

---------


 private View.OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener = new View.OnSystemUiVisibilityChangeListener() {
    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == View.VISIBLE) {
            mToolBar.startAnimation(mSlideDown);
            getSupportActionBar().show();
        } else {
            getSupportActionBar().hide();
            mToolBar.startAnimation(mSlideUp);
        }
    }
};

查找完整的sample code

答案 2 :(得分:1)

我在官方Google沉浸式示例项目代码中打开了一个新问题。希望他们能提供一种在沉浸式模式下处理工具栏的官方方法。

https://github.com/googlesamples/android-BasicImmersiveMode/issues/1