classSystemBarProtectionSnippets:ComponentActivity(){overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)// enableEdgeToEdge sets window.isNavigationBarContrastEnforced = true// which is used to add a translucent scrim to three-button navigationenableEdgeToEdge()setContent{MyTheme{// Main contentMyContent()// After drawing main content, draw status bar protectionStatusBarProtection()}}}}@ComposableprivatefunStatusBarProtection(color:Color=MaterialTheme.colorScheme.surfaceContainer,heightProvider:()->Float=calculateGradientHeight(),){Canvas(Modifier.fillMaxSize()){valcalculatedHeight=heightProvider()valgradient=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),)}}@ComposablefuncalculateGradientHeight():()->Float{valstatusBars=WindowInsets.statusBarsvaldensity=LocalDensity.currentreturn{statusBars.getTop(density).times(1.2f)}}
[null,null,["最后更新时间 (UTC):2025-08-23。"],[],[],null,["# About system bar protection\n\nOnce your app targets SDK 35 or later, [edge-to-edge is enforced](/about/versions/15/behavior-changes-15#edge-to-edge). The\nsystem status bar and gesture navigation bars are transparent, but the\nthree-button navigation bar is translucent. Call `enableEdgeToEdge` to make this\nbackwards compatible.\n\nHowever, the system defaults might not work for all use cases. Consult the\n[Android system bars design guidance](/design/ui/mobile/guides/foundations/system-bars) and [edge-to-edge design\nguidance](/design/ui/mobile/guides/layout-and-content/edge-to-edge) for an overview of when to consider having transparent or\ntranslucent system bars.\n\nCreate transparent system bars\n------------------------------\n\nCreate a transparent gesture navigation bar by targeting Android 15 or later or\nby calling `enableEdgeToEdge()` with default arguments for earlier versions. For\nthree-button navigation bar, set [`Window.setNavigationBarContrastEnforced`](/reference/android/view/Window#setNavigationBarContrastEnforced(boolean))\nto `false` otherwise there will be a translucent scrim applied.\n\nCreate translucent system bars\n------------------------------\n\nTo create a translucent status bar, create a custom composable that overlaps the\nmain content and draws a gradient in the area covered by insets.\n\n\n```kotlin\nclass SystemBarProtectionSnippets : ComponentActivity() {\n\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n\n // enableEdgeToEdge sets window.isNavigationBarContrastEnforced = true\n // which is used to add a translucent scrim to three-button navigation\n enableEdgeToEdge()\n\n setContent {\n MyTheme {\n // Main content\n MyContent()\n\n // After drawing main content, draw status bar protection\n StatusBarProtection()\n }\n }\n }\n}\n\n@Composable\nprivate fun StatusBarProtection(\n color: Color = MaterialTheme.colorScheme.surfaceContainer,\n heightProvider: () -\u003e Float = calculateGradientHeight(),\n) {\n\n Canvas(Modifier.fillMaxSize()) {\n val calculatedHeight = heightProvider()\n val gradient = Brush.verticalGradient(\n colors = listOf(\n color.copy(alpha = 1f),\n color.copy(alpha = .8f),\n Color.Transparent\n ),\n startY = 0f,\n endY = calculatedHeight\n )\n drawRect(\n brush = gradient,\n size = Size(size.width, calculatedHeight),\n )\n }\n}\n\n@Composable\nfun calculateGradientHeight(): () -\u003e Float {\n val statusBars = WindowInsets.statusBars\n val density = LocalDensity.current\n return { statusBars.getTop(density).times(1.2f) }\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/layouts/SystemBarProtectionSnippets.kt#L53-L103\n```\n\n\u003cbr /\u003e\n\n**Figure 1.** A translucent status bar.\n\nFor adaptive apps, insert a custom composable that matches the colors of each\npane, as seen in the [Edge-to--edge design](/design/ui/mobile/guides/layout-and-content/edge-to-edge). To create a translucent\nnavigation bar, set [`Window.setNavigationBarContrastEnforced`](/reference/android/view/Window#setNavigationBarContrastEnforced(boolean)) to true."]]