边到边模式下状态栏闪烁

问题描述 投票:0回答:1

我刚刚在新的 Android 项目上设置 Edge2Edge 模式。一般来说,这工作得很好,但是在应用程序启动时,有一个值得注意的时刻,状态栏根本不可见。

  1. 状态栏显示正常,同时显示应用程序徽标:

  1. 然后有一段时间状态栏没有或没有真正显示:

  1. 最后显示状态栏:

我这里有两个问题:

a) 状态栏第一次显示时“闪烁”,然后不显示,然后再次显示。

b) 状态栏颜色接近白色应用程序背景,但不是完全白色。

这就是

MainActivity
的样子:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        enableEdgeToEdge()


        setContent {
            NewToGetThemesTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize().safeDrawingPadding(),
                    color = MaterialTheme.colorScheme.background,

                ) {
                    Greeting("Android")
                }
            }
        }
    }
}

Theme.kt
看起来像这样(注意注释掉的副作用):

private val DarkColorScheme = darkColorScheme(
    primary = Purple80,
    secondary = PurpleGrey80,
    tertiary = Pink80
)

private val LightColorScheme = lightColorScheme(
    primary = Purple40,
    secondary = PurpleGrey40,
    tertiary = Pink40

    /* Other default colors to override
    background = Color(0xFFFFFBFE),
    surface = Color(0xFFFFFBFE),
    onPrimary = Color.White,
    onSecondary = Color.White,
    onTertiary = Color.White,
    onBackground = Color(0xFF1C1B1F),
    onSurface = Color(0xFF1C1B1F),
    */
)

@Composable
fun NewToGetThemesTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // Dynamic color is available on Android 12+
    dynamicColor: Boolean = true,
    content: @Composable () -> Unit
) {
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }

        darkTheme -> DarkColorScheme
        else -> LightColorScheme
    }
    val view = LocalView.current
    if (!view.isInEditMode) {
        SideEffect {
            val window = (view.context as Activity).window
            // I've commented out the next lines as they made everything worse
            //window.statusBarColor = colorScheme.primary.toArgb()
            //WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
        }
    }

    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography,
        content = content
    )
}

themes.xml
看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="Theme.NewToGetThemes" parent="android:Theme.Material.Light.NoActionBar" />
</resources>
android android-jetpack-compose android-theme
1个回答
0
投票

根据我对您问题的理解,我希望您能在本文中找到答案。 https://medium.com/@shivathapa.dev/enable-edge-to-edge-in-android-jetpack-compose-transparent-status-bar-89f565f3ed70

© www.soinside.com 2019 - 2024. All rights reserved.