简体   繁体   English

如何永久隐藏导航栏

[英]How to hide navigation bar permanently

如果我长按应用程序关闭的按钮,如何永久隐藏导航栏,否则应用程序处于全视图模式,没有导航栏隐藏从上到下滑动

https://developer.android.com/about/versions/android-4.0.html#SystemUI https://developer.android.com/about/versions/android-4.0.html#SystemUI

(Controls for system UI visibility section) : (系统 UI 可见性部分的控件):

The SYSTEM_UI_FLAG_HIDE_NAVIGATION is a new flag that requests the navigation bar hide completely. SYSTEM_UI_FLAG_HIDE_NAVIGATION是一个请求导航栏完全隐藏的新标志。 Be aware that this works only for the navigation bar used by some handsets (it does not hide the system bar on tablets).请注意,这仅适用于某些手机使用的导航栏(它不会隐藏平板电脑上的系统栏)。

However, you could try to dim the system bar as done sometimes during gaming and video playback.但是,您可以尝试像有时在游戏和视频播放期间一样使系统栏变暗。

The SYSTEM_UI_FLAG_LOW_PROFILE flag replaces the STATUS_BAR_HIDDEN flag. SYSTEM_UI_FLAG_LOW_PROFILE标志取代了 STATUS_BAR_HIDDEN 标志。 When set, this flag enables “low profile" mode for the system bar or navigation bar. Navigation buttons dim and other elements in the system bar also hide. Enabling this is useful for creating more immersive games without distraction for the system navigation buttons.设置后,此标志为系统栏或导航栏启用“低调”模式。导航按钮变暗,系统栏中的其他元素也会隐藏。启用此功能对于创建更加身临其境的游戏非常有用,而不会分散系统导航按钮的注意力。

Plz try this way请试试这个方法

There are two ways to hide the navigation bar:有两种方法可以隐藏导航栏:

Way 1:方式一:

Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”.轻触“设置”->“显示”->“导航栏”->“按钮”->“按钮布局”。 Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.在“隐藏导航栏”中选择图案 -> 当应用程序打开时,导航栏将自动隐藏,您可以从屏幕底角向上滑动以显示它。

Way 2:方式二:

Touch “Settings” -> “Display” -> “Navigation bar” -> “Fullscreen gestures”.点击“设置”->“显示”->“导航栏”->“全屏手势”。 The navigation bar will be hidden and you will be shown “Gesture demos” to explain how to operate the “Go back”, “Go to Home screen” and “Open Recents” functions.导航栏将被隐藏,您将看到“手势演示”,以解释如何操作“返回”、“返回主屏幕”和“打开最近”功能。

OR或者

Programetically以编程方式

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.第 1 步- 在 Android Studio 中创建一个新项目,转到 File ⇒ New Project 并填写所有必需的详细信息以创建一个新项目。

Step 2 − Add the following code to res/layout/activity_main.xml.第 2 步- 将以下代码添加到 res/layout/activity_main.xml。

  <?xml version="1.0" encoding="utf-8"?>
  <androidx.constraintlayout.widget.ConstraintLayout
     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:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".MainActivity">
       <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
  </androidx.constraintlayout.widget.ConstraintLayout>

Step 3 − Add the following code to src/MainActivity.java步骤 3 - 将以下代码添加到 src/MainActivity.java

 import androidx.appcompat.app.AppCompatActivity;
 import android.annotation.SuppressLint;
 import android.os.Build;
 import android.os.Bundle;
 import android.view.View;

 public class MainActivity extends AppCompatActivity {

 private int currentApiVersion;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  currentApiVersion = android.os.Build.VERSION.SDK_INT;
  final int flags = 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;
  if (currentApiVersion >= Build.VERSION_CODES.KITKAT) {
     getWindow().getDecorView().setSystemUiVisibility(flags);
     final View decorView = getWindow().getDecorView();
     decorView.setOnSystemUiVisibilityChangeListener(new 
     View.OnSystemUiVisibilityChangeListener() {
        @Override
        public void onSystemUiVisibilityChange(int visibility) {
           if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
              decorView.setSystemUiVisibility(flags);
           }
          }
       });
     }
  }
  @SuppressLint("NewApi")
  @Override
  public void onWindowFocusChanged(boolean hasFocus) {
  super.onWindowFocusChanged(hasFocus);
  if (currentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus) {
     
   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);
      }
   }
}

Step 4 − Add the following code to androidManifest.xml第 4 步- 将以下代码添加到 androidManifest.xml

       <?xml version="1.0" encoding="utf-8"?>
       <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
           package="app.com.sample">
            <application
                android:allowBackup="true"
                android:icon="@mipmap/ic_launcher"
                android:label="@string/app_name"
                android:roundIcon="@mipmap/ic_launcher_round"
                android:supportsRtl="true"
                android:theme="@style/AppTheme">
          <activity android:name=".MainActivity">
                <intent-filter>
                      <action android:name="android.intent.action.MAIN" />
                      <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
          </activity>
         </application>
    </manifest>

在此处输入图片说明

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

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