admin管理员组

文章数量:1277401

I am trying to create a true fullscreen edge-to-edge experience in my Android app using Jetpack Compose. The goal is to remove the status bar, extend my content into the area behind it, and make my app display seamlessly from edge to edge without any black space around the status bar or navigation bar.

See black spacing here where my camera sits.

Here is my activity

class TPMActivity : ComponentActivity() {

    private val settingsRepository: SettingsRepository by lazy {
        SettingsRepository(applicationContext)
    }
    private val settingsViewModel: SettingsViewModel by lazy {
        SettingsViewModel(settingsRepository)
    }

    @SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

        setContent {


            if (settings.showSystemUi) {
                showSystemUI(this)
            } else {
                hideSystemUI(this)
            }

            Scaffold(
                content = {
                    MyContent()
                }
            )
        }
    }
}

And here is my utils functions

fun hideSystemUI(activity: Activity) {
    WindowCompat.setDecorFitsSystemWindows(activity.window, false)
    WindowInsetsControllerCompat(activity.window, activity.window.decorView).let { controller ->
        controller.hide(WindowInsetsCompat.Type.systemBars())
        controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    }
}

fun showSystemUI(activity: Activity) {
    WindowCompat.setDecorFitsSystemWindows(activity.window, true)
    WindowInsetsControllerCompat(activity.window, activity.window.decorView).let { controller ->
        controller.hide(WindowInsetsCompat.Type.statusBars())
        controller.show(WindowInsetsCompat.Type.navigationBars())
    }
} 

本文标签: