撰写MDC主题适配器不正确转换每个颜色

发布于 2025-02-12 21:07:12 字数 4844 浏览 0 评论 0原文

我正在关注本指南转换我的xml构成主题的颜色。不幸的是,我无法获得正确的颜色。不知何故,我收到默认的Android调色板。

我测试了复选框,并切换了可组合的小部件。他们的有色颜色是错误的。我做错了吗?

对于复选框checkedcolor使用secondary。 对于Switch 检查bumbcolor使用secondaryVariant

<color name="ui7">#212B36</color>
    <item name="colorSecondary">@color/ui7</item>
    <item name="colorSecondaryVariant">@color/ui7</item>

但是,复选框CheckedColor看起来像黑色 然后开关检查thumbColor看起来像是默认的Android绿色。

怪异的部分是适配器适用于初级色。圆形刺激器上有正确的颜色。

myFragment.kt:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup): View {
    return ComposeView(container.context).apply {
        setContent {
            MainTheme {
                Scaffold(topBar = topAppbar("My Toolbar Title")) {
                    Column(
                        modifier = Modifier
                            .fillMaxSize()
                            .padding(it),
                        verticalArrangement = Arrangement.SpaceBetween,
                        horizontalAlignment = Alignment.CenterHorizontally,
                    ) {
                        Switch(
                            modifier = Modifier.padding(end = 8.dp),
                            checked = checkedState.value,
                            onCheckedChange = onValueChanged,
                        )
                        Checkbox(checked = true, onCheckedChange = onValueChanged)

                        RadioButton(selected = true, onClick = { { /*TODO*/ } })
                        OutlinedButton(onClick = {}) {
                            Text("Outlined Button")
                        }
                        FloatingActionButton(onClick = { /*TODO*/ }) {
                            Icon(
                                Icons.Filled.Favorite,
                                contentDescription = "Localized description"
                            )
                        }
                        CircularProgressIndicator(0.4f)

                    }
                }
            }
        }
    }
}

theme.kt:

@Composable
fun MainTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val context = LocalContext.current
    val layoutDirection = LocalLayoutDirection.current
    var (colors, type) = createMdcTheme(context, layoutDirection)
    MaterialTheme(
        colors = colors,
        typography = typography,
        shapes = shapes,
        content = content
    )
}

switch.kt:

object SwitchDefaults {
    @Composable
    fun colors(
        checkedThumbColor: Color = MaterialTheme.colors.secondaryVariant,
        checkedTrackColor: Color = checkedThumbColor,
        checkedTrackAlpha: Float = 0.54f,
        uncheckedThumbColor: Color = MaterialTheme.colors.surface,
        uncheckedTrackColor: Color = MaterialTheme.colors.onSurface,
        uncheckedTrackAlpha: Float = 0.38f,
        disabledCheckedThumbColor: Color = checkedThumbColor

checkbox.kt:

object CheckboxDefaults {
    /**
     * Creates a [CheckboxColors] that will animate between the provided colors according to the
     * Material specification.
     *
     * @param checkedColor the color that will be used for the border and box when checked
     * @param uncheckedColor color that will be used for the border when unchecked
     * @param checkmarkColor color that will be used for the checkmark when checked
     * @param disabledColor color that will be used for the box and border when disabled
     * @param disabledIndeterminateColor color that will be used for the box and
     * border in a [TriStateCheckbox] when disabled AND in an [ToggleableState.Indeterminate] state.
     */
    @Composable
    fun colors(
        checkedColor: Color = MaterialTheme.colors.secondary,
        uncheckedColor: Color = MaterialTheme.colors.onSurface.copy(alpha = 0.6f),
        checkmarkColor: Color = MaterialTheme.colors.surface,
        disabledColor: Color = MaterialTheme.colors.onSurface.copy(alpha = ContentAlpha.disabled),
        disabledIndeterminateColor: Color = checkedColor.copy(alpha = ContentAlpha.disabled)
    )

styles.xml: ”

I'm following this guide to convert my xml colors to Compose Theme. Unfortunately, I couldn't manage to get the correct colors. Somehow I receive the default android color palette.

I tested Checkbox and Switch Composable widgets. The tinted color of theirs is wrong. Am I doing something wrong?

For Checkbox checkedColor is using secondary.
For Switch checkedThumbColor is using secondaryVariant.

<color name="ui7">#212B36</color>
    <item name="colorSecondary">@color/ui7</item>
    <item name="colorSecondaryVariant">@color/ui7</item>

But, Checkbox checkedColor looks like it's black
and Switch checkedThumbColor looks like it's default android green.

The weird part is that the adapter works for primaryColor. CircularProgressIndicator has the correct color on it.

UI Screen

MyFragment.kt:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup): View {
    return ComposeView(container.context).apply {
        setContent {
            MainTheme {
                Scaffold(topBar = topAppbar("My Toolbar Title")) {
                    Column(
                        modifier = Modifier
                            .fillMaxSize()
                            .padding(it),
                        verticalArrangement = Arrangement.SpaceBetween,
                        horizontalAlignment = Alignment.CenterHorizontally,
                    ) {
                        Switch(
                            modifier = Modifier.padding(end = 8.dp),
                            checked = checkedState.value,
                            onCheckedChange = onValueChanged,
                        )
                        Checkbox(checked = true, onCheckedChange = onValueChanged)

                        RadioButton(selected = true, onClick = { { /*TODO*/ } })
                        OutlinedButton(onClick = {}) {
                            Text("Outlined Button")
                        }
                        FloatingActionButton(onClick = { /*TODO*/ }) {
                            Icon(
                                Icons.Filled.Favorite,
                                contentDescription = "Localized description"
                            )
                        }
                        CircularProgressIndicator(0.4f)

                    }
                }
            }
        }
    }
}

Theme.kt:

@Composable
fun MainTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val context = LocalContext.current
    val layoutDirection = LocalLayoutDirection.current
    var (colors, type) = createMdcTheme(context, layoutDirection)
    MaterialTheme(
        colors = colors,
        typography = typography,
        shapes = shapes,
        content = content
    )
}

Switch.kt:

object SwitchDefaults {
    @Composable
    fun colors(
        checkedThumbColor: Color = MaterialTheme.colors.secondaryVariant,
        checkedTrackColor: Color = checkedThumbColor,
        checkedTrackAlpha: Float = 0.54f,
        uncheckedThumbColor: Color = MaterialTheme.colors.surface,
        uncheckedTrackColor: Color = MaterialTheme.colors.onSurface,
        uncheckedTrackAlpha: Float = 0.38f,
        disabledCheckedThumbColor: Color = checkedThumbColor

Checkbox.kt:

object CheckboxDefaults {
    /**
     * Creates a [CheckboxColors] that will animate between the provided colors according to the
     * Material specification.
     *
     * @param checkedColor the color that will be used for the border and box when checked
     * @param uncheckedColor color that will be used for the border when unchecked
     * @param checkmarkColor color that will be used for the checkmark when checked
     * @param disabledColor color that will be used for the box and border when disabled
     * @param disabledIndeterminateColor color that will be used for the box and
     * border in a [TriStateCheckbox] when disabled AND in an [ToggleableState.Indeterminate] state.
     */
    @Composable
    fun colors(
        checkedColor: Color = MaterialTheme.colors.secondary,
        uncheckedColor: Color = MaterialTheme.colors.onSurface.copy(alpha = 0.6f),
        checkmarkColor: Color = MaterialTheme.colors.surface,
        disabledColor: Color = MaterialTheme.colors.onSurface.copy(alpha = ContentAlpha.disabled),
        disabledIndeterminateColor: Color = checkedColor.copy(alpha = ContentAlpha.disabled)
    )

styles.xml:
styles.xml

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文