您可以呼叫 enableEdgeToEdge
,在應用程式中啟用無邊框螢幕。對大多數應用程式來說應該都足夠。本指南說明如何在應用程式不需使用 enableEdgeToEdge
的情況下,啟用無邊框功能。
讓應用程式以全螢幕顯示
使用 WindowCompat.setDecorFitsSystemWindows(window,
false)
讓應用程式顯示在系統列後方,如以下程式碼範例所示:
Kotlin
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) WindowCompat.setDecorFitsSystemWindows(window, false) }
Java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); WindowCompat.setDecorFitsSystemWindows(getWindow(), false); }
變更系統資訊列的顏色
採用無邊框版面配置時,應用程式必須變更系統列的顏色,才能顯示下方內容。應用程式執行此步驟後,系統會在手勢操作模式和按鈕模式中處理使用者介面的所有視覺保護。
- 手勢操作模式:系統會套用動態色彩調整,配合系統列的內容根據背景內容改變色彩。在以下範例中,導覽列位於淺色內容上方時,導覽列的控點會變為深色,位於深色內容上方時,則會變成淺色。
- 按鈕模式:系統會於系統列 (API 級別 29 以上) 或透明系統列 (API 級別 28 以下版本) 後方套用半透明紗罩。
- 「狀態列內容顏色」:控制狀態列內容的顏色,例如時間和圖示。
您可以編輯 themes.xml
檔案,設定導覽列的顏色,並視需要將狀態列設為透明,狀態列的內容顏色則設為深色。
<!-- values-v29/themes.xml -->
<style name="Theme.MyApp">
<item name="android:navigationBarColor">
@android:color/transparent
</item>
<!-- Optional: set to transparent if your app is drawing behind the status bar. -->
<item name="android:statusBarColor">
@android:color/transparent
</item>
<!-- Optional: set for a light status bar with dark content. -->
<item name="android:windowLightStatusBar">
true
</item>
</style>
您可以直接使用 WindowInsetsController
API,但我們強烈建議盡可能使用支援資料庫 WindowInsetsControllerCompat
。您可以使用 WindowInsetsControllerCompat
API (而非 theme.xml
) 控制狀態列的內容顏色。方法是使用 setAppearanceLightNavigationBars()
函式傳入 true
,將導覽的前景顏色變更為淺色顏色,或用 false
還原為預設顏色。
Kotlin
val windowInsetsController = ViewCompat.getWindowInsetsController(window.decorView) windowInsetsController?.isAppearanceLightNavigationBars = true
Java
WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView()); if (windowInsetsController == null) { return; } windowInsetsController.setAppearanceLightNavigationBars(true);