简体   繁体   English

如何在 Android 应用中隐藏导航栏?

[英]How to hide navigation bar in Android app?

I was wondering how I could hide the navigation bar in an Android application?我想知道如何在 Android 应用程序中隐藏导航栏?

I know how to hide it initially, but as soon as I touch the screen it pops back up.一开始我知道如何隐藏它,但是一旦我触摸屏幕,它就会弹出。 I want to hide it the same way games like Clash of Clans hide it where the only way to make it pop up is by swiping down the notifications or by swiping where the navigation bar should be.我想像《部落冲突》这样的游戏隐藏它一样隐藏它,唯一让它弹出的方法是向下滑动通知或滑动导航栏应该在的位置。

use immersive mode check thisImmersive mode使用沉浸模式检查这个沉浸模式

    // 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);
}

Here it is in the context of the onCreate method:这是在 onCreate 方法的上下文中:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.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
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

    }

The Android docs have a good explanation of what the different flags do:Using Immersive Full-Screen Mode Android 文档对不同标志的作用有很好的解释:Using Immersive Full-Screen Mode

Just put this method in your activity where you want to hide status bar and navigation bar in sticky mode.只需将此方法放在您要在粘性模式下隐藏状态栏和导航栏的活动中即可。

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    //This is used to hide/show 'Status Bar' & 'System Bar'. Swip bar to get it as visible.
    View decorView = getWindow().getDecorView();
    if (hasFocus) {
        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
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }
}

the easy way is adding this 2 lines after super.onCreate(savedInstanceState);简单的方法是在 super.onCreate(savedInstanceState) 之后添加这 2 行;

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

In your activity's onCreate method:在您的活动的 onCreate 方法中:

this.getWindow()
    .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
              WindowManager.LayoutParams.FLAG_FULLSCREEN);

You Can Create An Activity With The Snippet Above,And Then Add Another Windows Above The Layout:您可以使用上面的代码段创建一个活动,然后在布局上方添加另一个窗口:

LayoutParams layoutParameteres=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        v.setLayoutParams(layoutParameteres);
        final WindowManager.LayoutParams parameters=new WindowManager.LayoutParams(sizeX, sizeY, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
        parameters.gravity=Gravity.CENTER;
        parameters.x=0;
        parameters.y=0;
        wm.addView(v, parameters);

Good Luck With It祝你好运

在 AndroidManifest.xml 文件中修改这一行

android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM