最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Material Theme not working on JetpackCompose - Stack Overflow

programmeradmin2浏览0评论

I have issues theming an Android App. I generated a theme with red source color in Material Theme Builder, and I downloaded the compose kt files and ovewrite my project files. I'm applying the theme correctly in my activity:

setContent {
        AppTheme {
            App(modifier = Modifier.fillMaxSize())
        }
    }

These are some of my colors:

val primaryLight = Color(0xFF904A41)
val onPrimaryLight = Color(0xFFFFFFFF)
val primaryContainerLight = Color(0xFFFFDAD5)
val onPrimaryContainerLight = Color(0xFF73342B)
val secondaryLight = Color(0xFF775652)
val onSecondaryLight = Color(0xFFFFFFFF)
val secondaryContainerLight = Color(0xFFFFDAD5)
val onSecondaryContainerLight = Color(0xFF5D3F3B)

Something is wrong, because I have some stuff like for example a NavigationSuiteScaffold and the buttons are grey instead of red. I also added a Button and it's blue instead of Red. Is there something I'm missing? I doublechecked the theming tuto also, and I'm doing every step.

This is for example how a Button is drawn, as you can see, is blue instead of red.

On the other hand, if I generate the theme with materialkolor then everything works and the colors are red. What happens with Material Theme Builder?

I have issues theming an Android App. I generated a theme with red source color in Material Theme Builder, and I downloaded the compose kt files and ovewrite my project files. I'm applying the theme correctly in my activity:

setContent {
        AppTheme {
            App(modifier = Modifier.fillMaxSize())
        }
    }

These are some of my colors:

val primaryLight = Color(0xFF904A41)
val onPrimaryLight = Color(0xFFFFFFFF)
val primaryContainerLight = Color(0xFFFFDAD5)
val onPrimaryContainerLight = Color(0xFF73342B)
val secondaryLight = Color(0xFF775652)
val onSecondaryLight = Color(0xFFFFFFFF)
val secondaryContainerLight = Color(0xFFFFDAD5)
val onSecondaryContainerLight = Color(0xFF5D3F3B)

Something is wrong, because I have some stuff like for example a NavigationSuiteScaffold and the buttons are grey instead of red. I also added a Button and it's blue instead of Red. Is there something I'm missing? I doublechecked the theming tuto also, and I'm doing every step.

This is for example how a Button is drawn, as you can see, is blue instead of red.

On the other hand, if I generate the theme with materialkolor.com then everything works and the colors are red. What happens with Material Theme Builder?

Share Improve this question edited Feb 7 at 19:59 NullPointerException asked Feb 7 at 19:42 NullPointerExceptionNullPointerException 37.6k80 gold badges230 silver badges402 bronze badges 2
  • Please also provide AppTheme and everything it depends on. – tyg Commented Feb 7 at 21:50
  • 1 My best guess is that you created the project with Material2 dependencies, and then you migrated to Material3 but did not resolve the import statements correctly. If possible, please provide the necessary code snippets. – Jay Commented Feb 7 at 21:52
Add a comment  | 

1 Answer 1

Reset to default 1

In your Theme.kt file, check if your composable AppTheme looks something like this:

@Composable
fun AppTheme(
    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 -> CustomLightColorScheme
    }

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

CustomLightColorScheme is your custom set of colors. It should be placed in the same file like LightColorScheme and DarkColorScheme.

private val CustomLightColorScheme = lightColorScheme(
    primary = primary,
    background = background,
    surface = surface,
    onSurfaceVariant = onSurfaceVariant,
)

Those primary, background, etc variables are your custom color variables from your Color.kt file.

Hope this solves it for you.

发布评论

评论列表(0)

  1. 暂无评论