隱藏狀態列
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本課程將說明如何在應用程式的不同版本
Android。隱藏狀態列 (及選用導覽列) 即可
會佔用更多顯示空間,從而提供更身歷其境的使用者體驗。
圖 1 顯示可見狀態列的應用程式:
圖 1. 顯示狀態列。
圖 2 顯示隱藏狀態列的應用程式。請注意,動作列也會隱藏。
請勿在沒有狀態列的情況下顯示動作列。
圖 2. 隱藏狀態列。
在 Android 4.0 以下版本中隱藏狀態列
您可以在 Android 4.0 (API 級別 14) 以下版本的裝置上隱藏狀態列,方法是設定
WindowManager
標記。您可以透過程式輔助或
請在應用程式的資訊清單檔案中設定活動主題。在應用程式的
如果狀態列必須始終保持開啟狀態,則建議採用資訊清單檔案
(不過嚴格說來,您可以透過程式輔助方式覆寫
第一個主題)。例如:
<application
...
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
...
</application>
使用活動主題的優點如下:
- 與透過程式輔助方式設定標記相比,維護容易且較不容易出錯。
- 這麼做可以提供更順暢的 UI 轉換體驗,因為系統能取得所需資訊
,在將應用程式的主要活動執行個體化之前,先算繪 UI。
您也可以透過程式輔助方式設定 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
旗標時 (無論是透過活動主題
程式輔助傳送) 的旗標,除非應用程式將其清除,否則標記會保持有效。
別擔心!您可以使用
FLAG_LAYOUT_IN_SCREEN
將活動版面配置設為使用啟用後可用的畫面區域
FLAG_FULLSCREEN
。這麼做可避免
隱藏及顯示內容。
在 Android 4.1 以上版本中隱藏狀態列
您可以在 Android 4.1 (API 級別 16) 以上版本中隱藏狀態列,方法是:
使用 setSystemUiVisibility()
。
setSystemUiVisibility()
會在
個別檢視層級都會匯總到整個視窗層級使用
setSystemUiVisibility()
:用來設定 UI 標記
能讓您更精細地控制系統資訊列
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
,協助您的應用程式維持
穩定的版面配置。
使用這個方法時,您有責任確保
應用程式的使用者介面 (例如 Google 地圖應用程式中的內建控制項) 並未提供
可能會被系統資訊列覆蓋這可能會導致應用程式無法使用。在大部分的情況下,
處理這種情況,方法是將 android:fitsSystemWindows
屬性新增至 XML 版面配置檔案,並設為
true
。這麼做會調整父項 ViewGroup
的邊框間距
為系統視窗預留空間對大多數應用程式來說,這已足夠使用完畢。
但在某些情況下,您可能需要修改預設邊框間距,才能取得所需的
設定應用程式的版面配置如要直接操控
版面配置相對於系統資訊列 (佔據視窗的
「內容插邊」),覆寫 fitSystemWindows(Rect insets)
。
fitSystemWindows()
方法是由
當視窗的內容插邊變更時為檢視區塊階層,讓視窗可以
並據此調整內容覆寫此方法即可處理
任意插播,因此應用程式的版面配置。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間: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."]]