简体   繁体   English

在 Jetpack Compose 中更改 android:windowBackground

[英]Change android:windowBackground in Jetpack Compose

I want to implement Splash Screen using Jetpack Compose.我想使用 Jetpack Compose 实现启动画面。 In the old View system, we can just change the android:windowBackground via XML Theme.在旧的视图系统中,我们可以通过 XML 主题更改android:windowBackground

How to do this in Compose?如何在 Compose 中做到这一点?

The Window Background is something specific to Android itself, it tells the system what background this activity draws "At All times".窗口背景是 Android 本身特有的东西,它告诉系统这个活动“始终”绘制什么背景。

For example, an activity with no view at all can still have a background, as it is defined in its theme.例如,一个根本没有视图的活动仍然可以有一个背景,正如它在其主题中定义的那样。 This is actually used for stuff like Splash Screens, as they have no UI, just a background.这实际上用于诸如启动画面之类的东西,因为它们没有 UI,只有背景。 Since the background of an activity takes no time to draw, while the UI can get delayed.由于 Activity 的背景不需要时间来绘制,而 UI 可能会延迟。

Compose is a UI framework, and any UI will only be drawn when it's ready, meaning it will only show when the activity is ready. Compose 是一个 UI 框架,任何 UI 只有在准备好时才会绘制,这意味着它只会在 Activity 准备好时才显示。 So a splash screen is impossible in Compose.因此,在 Compose 中不可能出现启动画面。 More like, it's unrelated.更像是,它是无关的。

If you just want to set a background to your entire UI, maybe put the entire UI in a Box with a background.如果您只想为整个 UI 设置背景,可以将整个 UI 放在带有背景的Box Or set the background colour in the MaterialTheme .或者在MaterialTheme设置背景颜色。 And if you're not using a theme, just make a common Composable that's just a Box如果你不使用主题,只需制作一个普通的可组合,它只是一个Box

As I looked into the AndroidManifest.xml , I saw that the app is still using old themes.xml for its activities.当我查看AndroidManifest.xml ,我发现该应用程序仍在使用旧的themes.xml进行其活动。

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppName">

From here I just edit the theme to apply a windowBackground从这里我只是编辑主题以应用windowBackground

<style name="Theme.AppName" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <item name="android:windowBackground">@color/black</item>
</style>

I did not create a new style and just use the main style.我没有创建新样式,只是使用主要样式。 It's enough to just apply a white Surface on the root composable to hide the splash background when the activity showed up.当活动出现时,只需在可组合的根上应用一个白色的Surface来隐藏飞溅背景就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM