應用程式指定目標為 SDK 35 以上版本時,系統會強制採用無邊框設計。系統狀態列和手勢操作列為透明,但三按鈕操作列為半透明。呼叫 enableEdgeToEdge
即可回溯相容。
不過,系統預設值可能不適用於所有用途。如要瞭解何時應考慮使用透明或半透明的系統資訊列,請參閱 Android 系統資訊列設計指南和無邊框設計指南。
建立透明的系統資訊列
如要建立透明的手勢操作導覽列,請指定 Android 15 以上版本,或針對舊版呼叫 enableEdgeToEdge()
並使用預設引數。如果是三按鈕導覽列,請將 Window.setNavigationBarContrastEnforced
設為 false
,否則系統會套用半透明的遮罩。
建立半透明系統資訊列
如要建立半透明狀態列,請建立自訂可組合函式,與主要內容重疊,並在插邊涵蓋的區域繪製漸層。
class SystemBarProtectionSnippets : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // enableEdgeToEdge sets window.isNavigationBarContrastEnforced = true // which is used to add a translucent scrim to three-button navigation enableEdgeToEdge() setContent { MyTheme { // Main content MyContent() // After drawing main content, draw status bar protection StatusBarProtection() } } } } @Composable private fun StatusBarProtection( color: Color = MaterialTheme.colorScheme.surfaceContainer, heightProvider: () -> Float = calculateGradientHeight(), ) { Canvas(Modifier.fillMaxSize()) { val calculatedHeight = heightProvider() val gradient = Brush.verticalGradient( colors = listOf( color.copy(alpha = 1f), color.copy(alpha = .8f), Color.Transparent ), startY = 0f, endY = calculatedHeight ) drawRect( brush = gradient, size = Size(size.width, calculatedHeight), ) } } @Composable fun calculateGradientHeight(): () -> Float { val statusBars = WindowInsets.statusBars val density = LocalDensity.current return { statusBars.getTop(density).times(1.2f) } }
如果是自適應應用程式,請插入與每個窗格顏色相符的自訂可組合項,如無邊框設計所示。如要建立半透明導覽列,請將 Window.setNavigationBarContrastEnforced
設為 true。