Ẩn thanh điều hướng
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Bài học này mô tả cách ẩn thanh điều hướng, đã được giới thiệu trong
Android 4.0 (API cấp 14).
Mặc dù bài học này tập trung vào việc che giấu
thanh điều hướng, bạn nên thiết kế ứng dụng để ẩn thanh trạng thái
đồng thời, như mô tả trong phần Ẩn thanh trạng thái.
Ẩn thanh điều hướng và thanh trạng thái (trong khi vẫn giữ cho các thanh này dễ truy cập)
cho phép nội dung sử dụng toàn bộ không gian hiển thị, do đó mang lại trải nghiệm
trải nghiệm người dùng.
Hình 1. Thanh điều hướng.
Ẩn thanh điều hướng
Bạn có thể ẩn thanh điều hướng bằng cách sử dụng
Cờ SYSTEM_UI_FLAG_HIDE_NAVIGATION
. Đoạn mã này ẩn cả
thanh điều hướng và thanh trạng thái:
Kotlin
window.decorView.apply {
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN
}
Java
View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
Xin lưu ý những điều sau:
- Với phương pháp này, việc chạm vào vị trí bất kỳ trên màn hình sẽ khiến thanh điều hướng (và
thanh trạng thái) xuất hiện lại và tiếp tục hiển thị. Sự tương tác của người dùng khiến cờ được
sẽ bị xoá.
- Sau khi cờ, ứng dụng cần đặt lại nếu bạn
lại muốn ẩn thanh. Hãy xem bài viết Cách phản hồi các thay đổi về chế độ hiển thị trên giao diện người dùng để biết
thảo luận về cách theo dõi các thay đổi về chế độ hiển thị giao diện người dùng để ứng dụng của bạn có thể
sẽ phản hồi phù hợp.
- Vị trí bạn đặt cờ giao diện người dùng tạo nên sự khác biệt. Nếu bạn ẩn các thanh hệ thống trong phần hoạt động của mình
onCreate()
và người dùng nhấn vào Màn hình chính, các thanh hệ thống sẽ
xuất hiện lại. Khi người dùng mở lại hoạt động, onCreate()
sẽ không được gọi, vì vậy thanh hệ thống sẽ vẫn hiển thị. Nếu bạn muốn thay đổi giao diện người dùng hệ thống thành
duy trì khi người dùng di chuyển vào và ra khỏi hoạt động của bạn, đặt cờ giao diện người dùng trong
onResume()
hoặc onWindowFocusChanged()
.
- Chỉ phương thức
setSystemUiVisibility()
có hiệu ứng nếu chế độ xem bạn dùng để gọi nó có thể nhìn thấy.
- Việc điều hướng ra khỏi khung hiển thị sẽ gây ra cờ
đặt bằng
setSystemUiVisibility()
cần xoá.
Hiển thị nội dung phía sau thanh điều hướng
Trên Android 4.1 trở lên, bạn có thể thiết lập để nội dung của ứng dụng xuất hiện phía sau
thanh điều hướng để nội dung không đổi kích thước khi thanh điều hướng ẩn đi và
các chương trình. Để làm việc này, hãy sử dụng
SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
.
Có thể bạn cũng cần phải sử dụng
SYSTEM_UI_FLAG_LAYOUT_STABLE
để giúp ứng dụng của bạn duy trì
bố cục ổn định.
Khi áp dụng phương pháp này, bạn có trách nhiệm đảm bảo rằng những phần quan trọng
giao diện người dùng của ứng dụng không bị thanh hệ thống che phủ. Để biết thêm
thảo luận về chủ đề này, hãy xem
Bài học về Ẩn Thanh trạng thái.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],null,["# Hide the navigation bar\n\nThis lesson describes how to hide the navigation bar, which was introduced in\nAndroid 4.0 (API level 14).\n\nEven though this lesson focuses on hiding the\nnavigation bar, you should design your app to hide the status bar\nat the same time, as described in [Hiding the Status Bar](/training/system-ui/status).\nHiding the navigation and status bars (while still keeping them readily accessible)\nlets the content use the entire display space, thereby providing a more immersive\nuser experience.\n\n**Figure 1.** Navigation bar.\n\nHide the Navigation Bar\n-----------------------\n\nYou can hide the navigation bar using the\n[SYSTEM_UI_FLAG_HIDE_NAVIGATION](/reference/android/view/View#SYSTEM_UI_FLAG_HIDE_NAVIGATION) flag. This snippet hides both\nthe navigation bar and the status bar: \n\n### Kotlin\n\n```kotlin\nwindow.decorView.apply {\n // Hide both the navigation bar and the status bar.\n // SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as\n // a general rule, you should design your app to hide the status bar whenever you\n // hide the navigation bar.\n systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN\n}\n```\n\n### Java\n\n```java\nView decorView = getWindow().getDecorView();\n// Hide both the navigation bar and the status bar.\n// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as\n// a general rule, you should design your app to hide the status bar whenever you\n// hide the navigation bar.\nint uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION\n | View.SYSTEM_UI_FLAG_FULLSCREEN;\ndecorView.setSystemUiVisibility(uiOptions);\n```\n\nNote the following:\n\n- With this approach, touching anywhere on the screen causes the navigation bar (and status bar) to reappear and remain visible. The user interaction causes the flags to be be cleared.\n- Once the flags have been cleared, 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\nMake Content Appear Behind the Navigation Bar\n---------------------------------------------\n\nOn Android 4.1 and higher, you can set your application's content to appear behind\nthe navigation bar, so that the content doesn't resize as the navigation bar hides and\nshows. To do this, use\n[SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION](/reference/android/view/View#SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION).\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 don't end up getting covered by system bars. For more\ndiscussion of this topic, see the [Hiding the Status Bar](/training/system-ui/status#behind) lesson."]]