Giảm độ sáng thanh hệ thống (không dùng nữa)
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 làm mờ thanh hệ thống (nghĩa là trạng thái và điều hướng
thanh) trên Android 4.0 (API cấp 14) trở lên. Android không cung cấp tính năng tích hợp sẵn để giảm độ sáng của
thanh hệ thống trên các phiên bản trước đó.
Khi bạn dùng phương pháp này, nội dung sẽ không đổi kích thước, mà sẽ đổi kích thước các biểu tượng trên thanh hệ thống
Lùi lại một cách trực quan. Ngay khi người dùng chạm vào thanh trạng thái hoặc khu vực thanh điều hướng của
màn hình, cả hai thanh đều hiển thị đầy đủ. Ưu điểm của phương pháp này
phương pháp tiếp cận là các thanh vẫn hiện diện nhưng chi tiết của chúng bị che khuất, do đó
tạo nên trải nghiệm sống động mà vẫn dễ dàng truy cập vào các quán bar.
Giảm độ sáng thanh trạng thái và thanh điều hướng
Bạn có thể làm mờ các thanh trạng thái và điều hướng bằng cách sử dụng
Cờ SYSTEM_UI_FLAG_LOW_PROFILE
như sau:
Kotlin
// This example uses decor view, but you can use any visible view.
activity?.window?.decorView?.apply {
systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE
}
Java
// This example uses decor view, but you can use any visible view.
View decorView = getActivity().getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE;
decorView.setSystemUiVisibility(uiOptions);
Ngay khi người dùng chạm vào thanh trạng thái hoặc điều hướng, cờ sẽ bị xoá,
khiến cho các thanh này không được đề cập. Sau khi cờ bị xoá, ứng dụng của bạn cần đặt lại
nếu bạn muốn giảm độ sáng của các thanh một lần nữa.
Hình 1 cho thấy một hình ảnh thư viện trong đó thanh điều hướng bị làm mờ (lưu ý rằng ứng dụng Thư viện
ẩn hoàn toàn thanh trạng thái; tính năng không giảm độ sáng). Lưu ý rằng thanh điều hướng (bên phải
của hình ảnh) có các chấm trắng mờ ở trên để thể hiện các công cụ điều hướng:
Hình 1. Thanh hệ thống bị làm mờ.
Hình 2 cho thấy cùng một hình ảnh thư viện, nhưng với các thanh hệ thống được hiển thị:
Hình 2. Thanh hệ thống hiển thị.
Hiển thị thanh trạng thái và điều hướng
Nếu bạn muốn xoá cờ được đặt bằng phương thức lập trình
setSystemUiVisibility()
, bạn có thể làm như vậy
như sau:
Kotlin
activity?.window?.decorView?.apply {
// Calling setSystemUiVisibility() with a value of 0 clears
// all flags.
systemUiVisibility = 0
}
Java
View decorView = getActivity().getWindow().getDecorView();
// Calling setSystemUiVisibility() with a value of 0 clears
// all flags.
decorView.setSystemUiVisibility(0);
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,["# Dim the system bars (deprecated)\n\n| **Deprecated:** [setSystemUiVisibility](/reference/android/view/View#setSystemUiVisibility(int)) is deprecated in API Level 30\n\nThis lesson describes how to dim the system bars (that is, the status and the navigation\nbars) on Android 4.0 (API level 14) and higher. Android does not provide a built-in way to dim the\nsystem bars on earlier versions.\n\nWhen you use this approach, the content doesn't resize, but the icons in the system bars\nvisually recede. As soon as the user touches either the status bar or the navigation bar area of\nthe screen, both bars become fully visible. The advantage of this\napproach is that the bars are still present but their details are obscured, thus\ncreating an immersive experience without sacrificing easy access to the bars.\n\nDim the Status and Navigation Bars\n----------------------------------\n\nYou can dim the status and navigation bars using the\n[SYSTEM_UI_FLAG_LOW_PROFILE](/reference/android/view/View#SYSTEM_UI_FLAG_LOW_PROFILE) flag, as follows: \n\n### Kotlin\n\n```kotlin\n// This example uses decor view, but you can use any visible view.\nactivity?.window?.decorView?.apply {\n systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE\n}\n```\n\n### Java\n\n```java\n// This example uses decor view, but you can use any visible view.\nView decorView = getActivity().getWindow().getDecorView();\nint uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE;\ndecorView.setSystemUiVisibility(uiOptions);\n```\n\nAs soon as the user touches the status or navigation bar, the flag is cleared,\ncausing the bars to be undimmed. Once the flag has been cleared, your app needs to reset\nit if you want to dim the bars again.\n\nFigure 1 shows a gallery image in which the navigation bar is dimmed (note that the Gallery app\ncompletely hides the status bar; it doesn't dim it). Notice that the navigation bar (right\nside of the image) has faint white dots on it to represent the navigation controls:\n\n\n**Figure 1.** Dimmed system bars.\n\nFigure 2 shows the same gallery image, but with the system bars displayed:\n\n\n**Figure 2.** Visible system bars.\n\nReveal the Status and Navigation Bars\n-------------------------------------\n\nIf you want to programmatically clear flags set with\n[setSystemUiVisibility()](/reference/android/view/View#setSystemUiVisibility(int)), you can do so\nas follows: \n\n### Kotlin\n\n```kotlin\nactivity?.window?.decorView?.apply {\n // Calling setSystemUiVisibility() with a value of 0 clears\n // all flags.\n systemUiVisibility = 0\n}\n```\n\n### Java\n\n```java\nView decorView = getActivity().getWindow().getDecorView();\n// Calling setSystemUiVisibility() with a value of 0 clears\n// all flags.\ndecorView.setSystemUiVisibility(0);\n```"]]