4

我想知道当用户点击屏幕时如何隐藏Status Bar,Navigation BarAndroid,如 QuickPick 应用程序所示:Toolbar

在此处输入图像描述

我做了很多研究,但我无法找到或猜测如何完成这种行为,有人可以解释一种方法吗?

4

1 回答 1

2

这是我的解决方案:

button.setOnClickListener(new View.OnClickListener() {

        boolean show = true;

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "CLICK", Toast.LENGTH_SHORT).show();
            if (show) {
                toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
                hideSystemUI();
                show = false;
            } else {
                toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();
                showSystemUI();
                show = true;
            }
        }
    });

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

}

private void showSystemUI() {
    View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);


}

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 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:id="@+id/rootView"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#000"
 android:fitsSystemWindows="true"
 tools:context=".MainActivity">
 .....................

不要忘记在主视图中将 fitSystemWindows 设置为 true

于 2018-06-16T20:28:44.637 回答