只要在搭載 Android 15 以上版本的裝置中指定 SDK 35 以上版本,您的應用程式就會顯示無邊框設計。在系統資訊列後方繪製,視窗會橫跨整個螢幕的寬度和高度。系統列包括狀態列、說明文字列和導覽列
許多應用程式都有頂端應用程式列。頂端應用程式列應延展至螢幕頂端邊緣,並顯示在狀態列後方。您可以選擇在內容捲動時,將頂端應用程式列縮小為狀態列的高度。
許多應用程式都有底部應用程式列或底部導覽列。此外,這些長條也應延展至螢幕底部邊緣,並顯示在導覽列後方。否則,應用程式應顯示在導覽列後方。
在應用程式中實作無邊框版面配置時,請注意下列事項:
- 啟用無邊框螢幕
- 處理視覺重疊。
- 建議你在系統資訊列後方顯示剪裁。
啟用無邊框螢幕
如果您的應用程式指定 SDK 35 以上版本,Android 15 或以上版本的裝置會自動啟用無邊框設計。
如要在先前的 Android 版本中啟用無邊框功能,請按照下列步驟操作:
請在應用程式或模組的
build.gradle
檔案中,將依附元件新增至androidx.activity
程式庫:Kotlin
dependencies { val activity_version =
activity_version
// Java language implementation implementation("androidx.activity:activity:$activity_version") // Kotlin implementation("androidx.activity:activity-ktx:$activity_version") }Groovy
dependencies { def activity_version =
activity_version
// Java language implementation implementation 'androidx.activity:activity:$activity_version' // Kotlin implementation 'androidx.activity:activity-ktx:$activity_version' }將
enableEdgeToEdge
擴充功能函式匯入應用程式:
在 Activity
的 onCreate
中呼叫 enableEdgeToEdge
,手動啟用無邊框功能。請在 setContentView
之前呼叫這個方法。
Kotlin
override fun onCreate(savedInstanceState: Bundle?) { enableEdgeToEdge() super.onCreate(savedInstanceState) ... }
Java
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { EdgeToEdge.enable(this); super.onCreate(savedInstanceState); ... }
根據預設,enableEdgeToEdge()
會將系統資訊列設為透明,但在三按鈕操作模式下,狀態列會變為半透明的薄板。系統圖示和薄板的顏色會根據系統淺色或深色主題進行調整。
enableEdgeToEdge()
函式會自動宣告應用程式應採用無邊框設計,並調整系統資訊列的顏色。
如要在不使用 enableEdgeToEdge()
函式的情況下,在應用程式中啟用無邊框螢幕功能,請參閱「手動設定無邊框螢幕」一文。
使用插邊處理重疊
應用程式的部分檢視畫面可能會在系統資訊列後方繪製,如圖 3 所示。
您可以回應插邊來解決重疊問題,指定畫面的哪些部分與系統 UI 交集,例如導覽列或狀態列。互動表示在內容上方顯示,但也可以讓應用程式瞭解系統手勢。
用來顯示無邊框應用程式插邊的插邊類型如下:
系統資訊列插邊:最適合可輕觸且不得被系統資訊列遮擋的檢視畫面。
螢幕凹口插邊:如果出現因裝置形狀而有螢幕凹口的區域,
系統手勢插邊:針對系統優先 (高於應用程式) 的手勢導覽區域。
系統資訊列插邊
系統列插邊是最常用的插邊類型,它們代表系統 UI 在應用程式上方的 Z 軸顯示的位置。最適合用來移動應用程式中可輕觸且不得被系統資訊列遮住的檢視畫面。
例如,圖 3 中的懸浮動作按鈕 (FAB) 遭到導覽列的部分內容遮蓋:
為避免在手勢模式或按鈕模式下發生這類視覺重疊,您可以使用 getInsets(int)
搭配 WindowInsetsCompat.Type.systemBars()
增加檢視畫面邊界。
以下程式碼範例顯示如何實作系統資訊列插邊:
Kotlin
ViewCompat.setOnApplyWindowInsetsListener(fab) { v, windowInsets -> val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) // Apply the insets as a margin to the view. This solution sets // only the bottom, left, and right dimensions, but you can apply whichever // insets are appropriate to your layout. You can also update the view padding // if that's more appropriate. v.updateLayoutParams<MarginLayoutParams> { leftMargin = insets.left, bottomMargin = insets.bottom, rightMargin = insets.right, } // Return CONSUMED if you don't want want the window insets to keep passing // down to descendant views. WindowInsetsCompat.CONSUMED }
Java
ViewCompat.setOnApplyWindowInsetsListener(fab, (v, windowInsets) -> { Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()); // Apply the insets as a margin to the view. This solution sets only the // bottom, left, and right dimensions, but you can apply whichever insets are // appropriate to your layout. You can also update the view padding if that's // more appropriate. MarginLayoutParams mlp = (MarginLayoutParams) v.getLayoutParams(); mlp.leftMargin = insets.left; mlp.bottomMargin = insets.bottom; mlp.rightMargin = insets.right; v.setLayoutParams(mlp); // Return CONSUMED if you don't want want the window insets to keep passing // down to descendant views. return WindowInsetsCompat.CONSUMED; });
如果您將這項解決方案套用至圖 3 所示的範例,按鈕模式就不會發生視覺重疊,如圖 4 所示:
這也適用於手勢操作模式,如圖 5 所示:
螢幕凹口插邊
部分裝置配備螢幕凹口。一般來說,凹口位於螢幕頂端,並且包含在狀態列中。當裝置螢幕處於橫向模式時,凹口可能會顯示在垂直邊緣。視應用程式在畫面上顯示的內容而定,您應該實作邊框間距,避免顯示螢幕凹口。根據預設,應用程式會在螢幕凹口中繪製。
舉例來說,許多應用程式畫面都會顯示項目清單。請勿用螢幕凹口或系統資訊列遮蓋清單項目。
Kotlin
ViewCompat.setOnApplyWindowInsetsListener(binding.recyclerView) { v, insets -> val bars = insets.getInsets( WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout() ) v.updatePadding( left = bars.left, top = bars.top, right = bars.right, bottom = bars.bottom, ) WindowInsetsCompat.CONSUMED }
Java
ViewCompat.setOnApplyWindowInsetsListener(mBinding.recyclerView, (v, insets) -> { WindowInsetsCompat bars = insets.getInsets( WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout() ); v.setPadding(bars.left, bars.top, bars.right, bars.bottom); return WindowInsetsCompat.CONSUMED; });
請取用系統資訊列和螢幕凹口類型的邏輯「或」,判斷 WindowInsetsCompat
的值。
將 clipToPadding
設為 RecyclerView
,讓邊框間距隨著清單項目捲動。如此一來,當使用者捲動網頁時,項目就能顯示在系統資訊列後方,如以下範例所示。
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
系統手勢插邊
系統手勢插邊代表系統手勢優先於應用程式的特定視窗區域。這些區域會以圖 6 的橘色顯示:
如同系統資訊列插邊,您可以搭配使用 getInsets(int)
和 WindowInsetsCompat.Type.systemGestures()
,避免重疊系統手勢插邊。
使用這些插邊將可滑動的檢視畫面從邊緣移動或滑過。常見的用途包括底部功能表、遊戲中的清除功能,以及使用 ViewPager2
實作的輪轉介面。
在 Android 10 以上版本中,系統手勢插邊包含主畫面手勢的底部插邊,以及返回手勢的左右插邊:
以下程式碼範例說明如何實作系統手勢插邊:
Kotlin
ViewCompat.setOnApplyWindowInsetsListener(view) { view, windowInsets -> val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemGestures()) // Apply the insets as padding to the view. Here, set all the dimensions // as appropriate to your layout. You can also update the view's margin if // more appropriate. view.updatePadding(insets.left, insets.top, insets.right, insets.bottom) // Return CONSUMED if you don't want the window insets to keep passing down // to descendant views. WindowInsetsCompat.CONSUMED }
Java
ViewCompat.setOnApplyWindowInsetsListener(view, (v, windowInsets) -> { Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemGestures()); // Apply the insets as padding to the view. Here, set all the dimensions // as appropriate to your layout. You can also update the view's margin if // more appropriate. view.setPadding(insets.left, insets.top, insets.right, insets.bottom); // Return CONSUMED if you don't want the window insets to keep passing down // to descendant views. return WindowInsetsCompat.CONSUMED; });
沉浸模式
某些內容在全螢幕模式中成效最佳,為使用者提供更身歷其境的體驗。您可以使用 WindowInsetsController
和 WindowInsetsControllerCompat
程式庫,在沉浸模式下隱藏系統資訊列:
Kotlin
val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView) // Hide the system bars. windowInsetsController.hide(Type.systemBars()) // Show the system bars. windowInsetsController.show(Type.systemBars())
Java
Window window = getWindow(); WindowInsetsControllerCompat windowInsetsController = WindowCompat.getInsetsController(window, window.getDecorView()); if (windowInsetsController == null) { return; } // Hide the system bars. windowInsetsController.hide(WindowInsetsCompat.Type.systemBars()); // Show the system bars. windowInsetsController.show(WindowInsetsCompat.Type.systemBars());
如要進一步瞭解如何實作這項功能,請參閱「在沉浸模式下隱藏系統資訊列」。
其他資源
如要進一步瞭解 WindowInsets
、手勢操作和插邊的運作方式,請參閱以下參考資料: