呼叫 enableEdgeToEdge 會封裝真正回溯相容所需的邏輯,因此建議您使用這種方式設定無邊框螢幕。如要瞭解使用 enableEdgeToEdge 實現無邊框設計的新方法,請參閱 Compose 和檢視畫面說明文件,而非本指南。
雖然不建議這麼做,但如果應用程式必須手動設定無邊框螢幕,請按照下列步驟操作:
- 呼叫
WindowCompat.setDecorFitsSystemWindows(window, false)。 - 將系統資訊列設為透明。
- 處理插邊。
以全螢幕模式顯示應用程式
使用 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); }
變更系統資訊列的顏色
在 Android 14 以下版本中,手動建立無邊框版面配置時,應用程式也必須將系統資訊列設為透明。
您可以編輯 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>
您可以使用 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);
處理插邊
最後,應用程式必須處理插邊,避免重要 UI 與系統資訊列和螢幕凹口重疊。請參閱 Compose 和檢視畫面說明文件,瞭解如何處理插邊。