by Anonymous » 22 Aug 2025, 08:24
Android 15 schlägt vor, EnableedGetOedge () zu verwenden, aber dies beseitigt den oberen Versatz der Statusleiste, wodurch sich der Hauptinhalt überlappt. Lassen Sie es einfach transparent und rendern Sie den Inhalt darunter.
Code: Select all
// in the MainActivity.onCreate():
enableEdgeToEdge()
// the callback will be invoked repeatedly, use this flag to only set the margin-top once
var offsetAdjusted = false
window.decorView.setOnApplyWindowInsetsListener { _, insets ->
if (!offsetAdjusted) {
offsetAdjusted = true
val top = if(Build.VERSION.SDK_INT > Def.ANDROID_10) // for android > 10
insets.getInsetsIgnoringVisibility(WindowInsets.Type.statusBars()).top
else // for android 10
view.rootWindowInsets.stableInsetTop
// the fragment container, which is a FragmentContainerView
val container = binding.root.findViewById(R.id.nav_host_fragment_activity_main)
val params = container.layoutParams as ViewGroup.MarginLayoutParams
params.topMargin = top
container.layoutParams = params
}
insets
}
Android 15 schlägt vor, EnableedGetOedge () zu verwenden, aber dies beseitigt den oberen Versatz der Statusleiste, wodurch sich der Hauptinhalt überlappt. Lassen Sie es einfach transparent und rendern Sie den Inhalt darunter.[code]// in the MainActivity.onCreate():
enableEdgeToEdge()
// the callback will be invoked repeatedly, use this flag to only set the margin-top once
var offsetAdjusted = false
window.decorView.setOnApplyWindowInsetsListener { _, insets ->
if (!offsetAdjusted) {
offsetAdjusted = true
val top = if(Build.VERSION.SDK_INT > Def.ANDROID_10) // for android > 10
insets.getInsetsIgnoringVisibility(WindowInsets.Type.statusBars()).top
else // for android 10
view.rootWindowInsets.stableInsetTop
// the fragment container, which is a FragmentContainerView
val container = binding.root.findViewById(R.id.nav_host_fragment_activity_main)
val params = container.layoutParams as ViewGroup.MarginLayoutParams
params.topMargin = top
container.layoutParams = params
}
insets
}
[/code]