Android 15 強制開啟 Edge-to-Edge 模式。應用程式會預設填滿整個螢幕,包含狀態列與導覽列下方。如果你的 FrameLayout 蓋住了系統欄,需要手動處理 Insets(視窗內邊距)。1
使用 fitsSystemWindows
這是最快的方法。在 XML 佈局中,對最外層的 FrameLayout 加入屬性:
XML
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
</FrameLayout>
這會讓系統自動幫 FrameLayout 加上內邊距,避開狀態列與導覽列。
程式碼精確控制
如果你只需要頂部避開狀態列,或是想保留背景填滿全螢幕,建議在 Activity 中使用 ViewCompat 監聽器。
Kotlin
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.your_frame_layout)) { view, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
// 根據需求設定 padding
view.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
這段程式碼會抓取系統欄的高度。將它設為 FrameLayout 的 padding 後,內容就不會被遮擋。
暫時停用強制規範
如果你還沒準備好適應新規則,可以在 Android 15 (API 35) 的主題中加入這個屬性。但注意這只是過渡方案,未來版本可能失效。2
在 res/values-v35/themes.xml 加入:
XML
<item name="android:windowOptOutEdgeToEdgeEnforcement">true</item>
這會讓 App 回到舊版的顯示邏輯,不再自動延伸到系統欄下方。
Handling Edge-to-Edge in Android 15
這段影片詳細解說了 Android 15 強制全螢幕的原理,並示範如何在 Compose 和傳統 XML 中正確處理系統內邊距。