隐藏状态栏
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本课介绍如何隐藏不同版本的
Android。通过隐藏状态栏(也可选择隐藏导航栏),
内容会占用更多的显示空间,从而提供沉浸感更强的用户体验。
图 1 显示了具有可见状态栏的应用:
图 1. 状态栏可见。
图 2 显示了状态栏已隐藏的应用。请注意,操作栏也会隐藏。
切勿在没有状态栏的情况下显示操作栏。
图 2. 状态栏隐藏。
在 Android 4.0 及更低版本中隐藏状态栏
在 Android 4.0(API 级别 14)及更低版本上,您可以通过将状态栏设置为
WindowManager
标志。您可以通过编程方式或通过
在应用的清单文件中设置 activity 主题。在应用的
如果状态栏应始终保持
(但严格来说,您可以通过编程方式替换
主题)。例如:
<application
...
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
...
</application>
使用 Activity 主题背景的优势如下:
- 与通过编程方式设置标志相比,更易于维护且不易出错。
- 这会使界面过渡更顺畅,因为系统拥有所需的信息
先呈现界面,然后再实例化应用的主 activity。
或者,您可以通过编程方式设置 WindowManager
标志。
此方法可在用户与以下应用互动时更轻松地隐藏和显示状态栏
您的应用:
Kotlin
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// If the Android version is lower than Jellybean, use this call to hide
// the status bar.
if (Build.VERSION.SDK_INT < 16) {
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
setContentView(R.layout.activity_main)
}
...
}
Java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If the Android version is lower than Jellybean, use this call to hide
// the status bar.
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(R.layout.activity_main);
}
...
}
当您设置 WindowManager
标志时(无论是通过 activity 主题还是
则标志将保持有效,除非您的应用将其清除。
您可以使用
FLAG_LAYOUT_IN_SCREEN
将 activity 布局设为使用与启用
FLAG_FULLSCREEN
。这样可以防止您的
禁止在状态栏隐藏和显示时调整大小。
在 Android 4.1 及更高版本中隐藏状态栏
在 Android 4.1(API 级别 16)及更高版本中,您可以隐藏状态栏,方法是
使用 setSystemUiVisibility()
。
setSystemUiVisibility()
将界面标志设置为
单个视图级别这些设置会汇总到窗口级别。使用
setSystemUiVisibility()
,用于设置界面标志
与使用 Google Kubernetes Engine 时
WindowManager
标志。以下代码段会隐藏状态栏:
Kotlin
// Hide the status bar.
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
actionBar?.hide()
Java
View decorView = getWindow().getDecorView();
// Hide the status bar.
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();
请注意以下几点:
让内容显示在状态栏后面
在 Android 4.1 及更高版本中,您可以将应用的内容设置为显示在后面
这样,内容就不会随着状态栏的隐藏和显示调整大小。
为此,请使用
SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
。
您可能还需要使用
SYSTEM_UI_FLAG_LAYOUT_STABLE
来帮助您的应用保持
稳定的布局。
使用此方法时,您有责任确保关键部分
(例如,地图应用中的内置控件)
被系统栏覆盖否则会导致您的应用无法使用。在大多数情况下,您可以
具体做法是,向 XML 布局文件添加 android:fitsSystemWindows
属性,将其设为
true
。这会调整父级 ViewGroup
的内边距
为系统窗口留出空间这对于大多数应用来说已经足够。
但在某些情况下,您可能需要修改默认内边距,以获得所需的
应用布局要直接控制
内容的布局方式是相对于系统栏
"content insets"),请替换 fitSystemWindows(Rect insets)
。
fitSystemWindows()
方法是由
视图层次结构,以允许窗口
并据此调整其内容。通过重写此方法,您可以处理
边衬区(也就是应用的布局)。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Hide the status bar\n\nThis lesson describes how to hide the status bar on different versions of\nAndroid. Hiding the status bar (and optionally, the navigation bar) lets the\ncontent use more of the display space, thereby providing a more immersive user experience.\n\n\nFigure 1 shows an app with a visible status bar:\n\n**Figure 1.** Visible status bar.\n\n\nFigure 2 shows an app with a hidden status bar. Note that the action bar is hidden too.\nYou should never show the action bar without the status bar.\n\n**Figure 2.** Hidden status bar.\n\nHide the Status Bar on Android 4.0 and Lower\n--------------------------------------------\n\nYou can hide the status bar on Android 4.0 (API level 14) and lower by setting\n[WindowManager](/reference/android/view/WindowManager) flags. You can do this programmatically or by\nsetting an activity theme in your app's manifest file. Setting an activity theme in your app's\nmanifest file is the preferred approach if the status bar should always remain\nhidden in your app (though strictly speaking, you could programmatically override the\ntheme if you wanted to). For example: \n\n```xml\n\u003capplication\n ...\n android:theme=\"@android:style/Theme.Holo.NoActionBar.Fullscreen\" \u003e\n ...\n\u003c/application\u003e\n```\n\nThe advantages of using an activity theme are as follows:\n\n- It's easier to maintain and less error-prone than setting a flag programmatically.\n- It results in smoother UI transitions, because the system has the information it needs to render your UI before instantiating your app's main activity.\n\n\nAlternatively, you can programmatically set [WindowManager](/reference/android/view/WindowManager) flags.\nThis approach makes it easier to hide and show the status bar as the user interacts with\nyour app: \n\n### Kotlin\n\n```kotlin\nclass MainActivity : Activity() {\n\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n // If the Android version is lower than Jellybean, use this call to hide\n // the status bar.\n if (Build.VERSION.SDK_INT \u003c 16) {\n window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,\n WindowManager.LayoutParams.FLAG_FULLSCREEN)\n }\n setContentView(R.layout.activity_main)\n }\n ...\n}\n```\n\n### Java\n\n```java\npublic class MainActivity extends Activity {\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n // If the Android version is lower than Jellybean, use this call to hide\n // the status bar.\n if (Build.VERSION.SDK_INT \u003c 16) {\n getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,\n WindowManager.LayoutParams.FLAG_FULLSCREEN);\n }\n setContentView(R.layout.activity_main);\n }\n ...\n}\n```\n\nWhen you set [WindowManager](/reference/android/view/WindowManager) flags (whether through an activity theme or\nprogrammatically), the flags remain in effect unless your app clears them.\n\nYou can use\n[FLAG_LAYOUT_IN_SCREEN](/reference/android/view/WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN)\nto set your activity layout to use the same screen area that's available when you've enabled\n[FLAG_FULLSCREEN](/reference/android/view/WindowManager.LayoutParams#FLAG_FULLSCREEN). This prevents your\ncontent from resizing when the status bar hides and shows.\n\nHide the Status Bar on Android 4.1 and Higher\n---------------------------------------------\n\nYou can hide the status bar on Android 4.1 (API level 16) and higher by\nusing [setSystemUiVisibility()](/reference/android/view/View#setSystemUiVisibility(int)).\n[setSystemUiVisibility()](/reference/android/view/View#setSystemUiVisibility(int)) sets UI flags at\nthe individual view level; these settings are aggregated to the window level. Using\n[setSystemUiVisibility()](/reference/android/view/View#setSystemUiVisibility(int)) to set UI flags\ngives you more granular control over the system bars than using\n[WindowManager](/reference/android/view/WindowManager) flags. This snippet hides the status bar: \n\n### Kotlin\n\n```kotlin\n// Hide the status bar.\nwindow.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN\n// Remember that you should never show the action bar if the\n// status bar is hidden, so hide that too if necessary.\nactionBar?.hide()\n```\n\n### Java\n\n```java\nView decorView = getWindow().getDecorView();\n// Hide the status bar.\nint uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;\ndecorView.setSystemUiVisibility(uiOptions);\n// Remember that you should never show the action bar if the\n// status bar is hidden, so hide that too if necessary.\nActionBar actionBar = getActionBar();\nactionBar.hide();\n```\n\nNote the following:\n\n- 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 [Responding to UI Visibility Changes](/training/system-ui/visibility) for a discussion of how to listen for UI visibility changes so that your app can respond accordingly.\n- Where you set the UI flags makes a difference. If you hide the system bars in your activity's [onCreate()](/reference/android/app/Activity#onCreate(android.os.Bundle)) method and the user presses Home, the system bars will reappear. When the user reopens the activity, [onCreate()](/reference/android/app/Activity#onCreate(android.os.Bundle)) 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()](/reference/android/app/Activity#onResume()) or [onWindowFocusChanged()](/reference/android/view/Window.Callback#onWindowFocusChanged(boolean)).\n- The method [setSystemUiVisibility()](/reference/android/view/View#setSystemUiVisibility(int)) only has an effect if the view you call it from is visible.\n- Navigating away from the view causes flags set with [setSystemUiVisibility()](/reference/android/view/View#setSystemUiVisibility(int)) to be cleared.\n\n\u003cbr /\u003e\n\nMake Content Appear Behind the Status Bar\n-----------------------------------------\n\nOn Android 4.1 and higher, you can set your application's content to appear behind\nthe status bar, so that the content doesn't resize as the status bar hides and shows.\nTo do this, use\n[SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN](/reference/android/view/View#SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN).\nYou may also need to use\n[SYSTEM_UI_FLAG_LAYOUT_STABLE](/reference/android/view/View#SYSTEM_UI_FLAG_LAYOUT_STABLE) to help your app maintain a\nstable layout.\n\nWhen you use this approach, it becomes your responsibility to ensure that critical parts\nof your app's UI (for example, the built-in controls in a Maps application) don't end up\ngetting covered by system bars. This could make your app unusable. In most cases you can\nhandle this by adding the `android:fitsSystemWindows` attribute to your XML layout file, set to\n`true`. This adjusts the padding of the parent [ViewGroup](/reference/android/view/ViewGroup)\nto leave space for the system windows. This is sufficient for most applications.\n\nIn some cases, however, you may need to modify the default padding to get the desired\nlayout for your app. To directly manipulate how your\ncontent lays out relative to the system bars (which occupy a space known as the window's\n\"content insets\"), override [fitSystemWindows(Rect insets)](/reference/android/view/View#fitSystemWindows(android.graphics.Rect)).\nThe [fitSystemWindows()](/reference/android/view/View#fitSystemWindows(android.graphics.Rect)) method is called by the\nview hierarchy when the content insets for a window have changed, to allow the window to\nadjust its content accordingly. By overriding this method you can handle the\ninsets (and hence your app's layout) however you want."]]