错误当尝试切换到黑暗的主题在android studio

ctehm74n  于 2023-01-04  发布在  Android
关注(0)|答案(2)|浏览(85)

尝试在android studio中更改为dark主题时出错,尝试更改主题(使用代码)时状态未发生更改,文本和背景均未更改
这是密码:

@Composable
fun MyCompnent(){
    Row(modifier = Modifier
        .background(MaterialTheme.colors.background)
        .padding(8.dp)) {
        MyImageView()
        MyTexts()
    }
}
@Composable
fun MyTexts(){
    Column(modifier = Modifier.padding(start = 5.dp)) {
        MyText(
            "HELLO",
            MaterialTheme.colors.primary,
            MaterialTheme.typography.subtitle1
        )
        Spacer(modifier = Modifier.height(9.dp))
        MyText(
            "WORLD",
            MaterialTheme.colors.onBackground,
            MaterialTheme.typography.subtitle2
        )
    }
}
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun PreviewComposable(){
    MyCompnent()
}
package com.example.tomasardiles.ui.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.MaterialTheme
import androidx.compose.material.darkColors
import androidx.compose.material.lightColors
import androidx.compose.runtime.Composable

private val DarkColorPalette = darkColors(
    primary = Purple200,
    primaryVariant = Purple700,
    secondary = Teal200,
    onBackground = white
)

private val LightColorPalette = lightColors(
    primary = Purple500,
    primaryVariant = Purple700,
    secondary = Teal200,
    onBackground = black
)
package com.example.tomasardiles.ui.theme
import androidx.compose.ui.graphics.Color

val Purple200 = Color(0xFFBB86FC)
val Purple500 = Color(0xFF6200EE)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
val white = Color.White
val black = Color.Black

我按照一门课程编写了代码,我所期望的是背景会变成黑色,单词"WORLD"会变成白色

vnzz0bqm

vnzz0bqm1#

**更新更新

一旦它进入黑暗模式,一旦用户界面模式似乎工作?

**更新

我不得不在预览中手动将其翻转到黑暗模式。我没有发现uimode可以工作:

@Preview
@Composable
fun DefaultPreview() {
    MyApplicationTheme(darkTheme = true) {
        Surface {
            Greeting("Android")

        }
    }
}

看看如何定义你的主题。

@Composable
fun MyTheme(useDarkTheme : Boolean, content : @Composable () -> Unit){
  MaterialTheme(colors : Colors = if(useDarkTheme) darkPalette else lightPalette, content = content)
}
njthzxwz

njthzxwz2#

尝试使用AppTheme可组合函数更新Theme文件,如下所示:

package com.example.tomasardiles.ui.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.MaterialTheme
import androidx.compose.material.darkColors
import androidx.compose.material.lightColors
import androidx.compose.runtime.Composable

private val DarkColorPalette = darkColors(
    primary = Purple200,
    primaryVariant = Purple700,
    secondary = Teal200,
    onBackground = white
)

private val LightColorPalette = lightColors(
    primary = Purple500,
    primaryVariant = Purple700,
    secondary = Teal200,
    onBackground = black
)

@Composable
fun AppTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit) {
    val colors = if (darkTheme) {
        DarkColorPalette
    } else {
        LightColorPalette
    }

    MaterialTheme(
        colorScheme = colors
    )
}

然后在MainActivity文件中添加以下AppTheme

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AppTheme {
                MyCompnent()
            }
        }
    }

相关问题