Android - Hide the status bar
发布日期:2021-05-06 23:02:42 浏览次数:21 分类:精选文章

本文共 8126 字,大约阅读时间需要 27 分钟。

Hide the Status Bar on Android 4.1 and Higher

You can hide the status bar on Android 4.1 (API level 16) and higher by using setSystemUiVisibility(). This method sets UI flags at the individual view level; these settings are aggregated to the window level. Using setSystemUiVisibility() provides more granular control over the system bars compared to using WindowManager flags. The following snippet hides the status bar:

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

Notes:

  • Once UI flags have been cleared (for example, by navigating away from the activity), your app needs to reset them if you want to hide the bars again. See for a discussion of how to listen for UI visibility changes so that your app can respond accordingly.

  • Where you set the UI flags makes a difference. If you hide the system bars in your activity’s onCreate() method and the user presses Home, the system bars will reappear. When the user reopens the activity, onCreate() won’t get called, so the system bars will remain visible. If you want system UI changes to persist as the user navigates in and out of your activity, set UI flags in onResume() or onWindowFocusChanged().

  • The method setSystemUiVisibility() only has an effect if the view you call it from is visible.

  • Navigating away from the view causes flags set with setSystemUiVisibility() to be cleared.


  • Make Content Appear Behind the Status Bar

    On Android 4.1 and higher, you can set your application’s content to appear behind the status bar, so that the content doesn’t resize as the status bar hides and shows. To do this, use SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN. You may also need to use SYSTEM_UI_FLAG_LAYOUT_STABLE to help your app maintain a stable layout.

    When using this approach, it becomes your responsibility to ensure that critical parts of your app’s UI (for example, the built-in controls in a Maps application) don’t end up getting covered by system bars. This could make your app unusable. In most cases, you can handle this by adding the android:fitsSystemWindows attribute to your XML layout file, set to true. This adjusts the padding of the parent ViewGroup to leave space for the system windows. This is sufficient for most applications.

    In some cases, however, you may need to modify the default padding to get the desired layout for your app. To directly manipulate how your content lays out relative to the system bars (which occupy a space known as the window’s “content insets”), override fitSystemWindows(Rect insets). The fitSystemWindows() method is called by the view hierarchy when the content insets for a window have changed, allowing the window to adjust its content accordingly. By overriding this method, you can handle the insets (and hence your app’s layout) however you want.


    Project Preparation

    Development Environment:

    • IDE: Android Studio 4.1.1
    • Build #: #AI-201.8743.12.41.6953283, built on November 5, 2020
    • Runtime Version: 1.8.0_242-release-1644-b01 (AMD64)
    • VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
    • Operating System: Windows 10 10.0

    Android Virtual Devices:

    • Name: Pixel 2
    • CPU/ABI: Google Play Intel Atom (x86)
    • Path: C:\Users\86188.android\avd\Pixel_2_API_28.avd
    • Target: google_apis_playstore [Google Play] (API level 28)
    • Skin: pixel_2
    • SD Card: 512MB
    • fastboot.chosenSnapshotFile: runtime.network.speed: full
    • hw.accelerometer: yes
    • hw.device.name: pixel_2
    • hw.lcd.width: 1080
    • hw.initialOrientation: Portrait
    • image.androidVersion.api: 28
    • tag.id: google_apis_playstore
    • hw.cpu.ncore: 4
    • hw.keyboard: yes
    • hw.sensors.proximity: yes
    • hw.dPad: no
    • hw.lcd.height: 1920
    • vm.heapSize: 256
    • skin.dynamic: yes
    • hw.device.manufacturer: Google
    • hw.gps: yes
    • audioInput: yes
    • image.sysdir.1: system-images\android-28\google_apis_playstore\x86
    • showDeviceFrame: yes
    • camera.back: virtual
    • sceneAvdId: Pixel_2_API_28
    • hw.lcd.density: 420
    • hw.arc: false
    • hw.device.hash2: MD5: 55acbc835978f326788ed66a5cd4c9a7
    • fastboot.forceColdBoot: no
    • fastboot.forceFastBoot: yes
    • hw.trackBall: no
    • hw.battery: yes
    • hw.sdCard: yes
    • tag.display: Google Play
    • runtime.network.latency: none
    • disk.dataPartition.size: 6442450944
    • hw.sensors.orientation: yes
    • avd.ini.encoding: UTF-8
    • hw.gpu.enabled: yes

    Project Creation:

    • Effect: [Insert image link]
    • New Project: Select Empty Activity, and in project configuration, set Minimum SDK to API 16: Android 4.1 (Jelly Bean)
    • New Activity: Create a new Empty Activity, named SecondaryActivity
    • Layout File Editing: Modify src\main\res\layout\activity_secondary.xml to add Toolbar and Button components

    Activity Configuration:

    • Layout File: src\main\res\layout\activity_secondary.xml

    • Activity Code: Modify SecondaryActivity.java

      package com.mk;
      import androidx.appcompat.app.AppCompatActivity;
      import androidx.appcompat.widget.Toolbar;
      import android.os.Build;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
      public class SecondaryActivity extends AppCompatActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_secondary);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      Button buttonMakeContentAppearBehindTheStatusBar = (Button) findViewById(R.id.buttonMakeContentAppearBehindTheStatusBar);
      buttonMakeContentAppearBehindTheStatusBar.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      View decorView = getWindow().getDecorView();
      decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
      }
      }
      });
      }
      }

    Main Activity Configuration:

    • Layout File: src\main\res\layout\activity_main.xml

    • Activity Code: Modify MainActivity.java

      package com.mk;
      import androidx.appcompat.app.AppCompatActivity;
      import androidx.appcompat.widget.Toolbar;
      import android.content.Intent;
      import android.os.Build;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
      public class MainActivity extends AppCompatActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      Button buttonHideTheStatusBar = (Button) findViewById(R.id.buttonHideTheStatusBar);
      buttonHideTheStatusBar.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      View decorView = getWindow().getDecorView();
      decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
      getSupportActionBar().hide();
      }
      }
      });
      Button buttonToSecondaryActivity = (Button) findViewById(R.id.buttonToSecondaryActivity);
      buttonToSecondaryActivity.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      Intent intent = new Intent(MainActivity.this, SecondaryActivity.class);
      startActivity(intent);
      }
      });
      }
      }

    Application Manifest:

    Modify src\main\AndroidManifest.xml:

  • Set android:theme to @style/Theme.AppCompat.Light.NoActionBar to prevent the app from using the default ActionBar (11th row).
  • Establish parent-child relationships for SecondaryActivity and MainActivity (12th–14th rows).

  • References:

    上一篇:Android - Hide the navigation bar
    下一篇:Android - Dim the system bars

    发表评论

    最新留言

    表示我来过!
    [***.240.166.169]2025年04月17日 21时33分16秒