撰写MDC主题适配器不正确转换每个颜色
我正在关注本指南转换我的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)
)
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.
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)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论