无法在材料设计中使用lotcontentalpha更改文本重点3

发布于 2025-02-06 17:08:48 字数 528 浏览 2 评论 0 原文

目前,我将我的一个应用程序之一迁移到材料设计3,该材料设计3完全使用JetPack Copose迁移。

在使用材料设计2时,我能够使用以下代码更改文本的重点。

CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
                Text(
                    text = "Hello, world",
                    style = MaterialTheme.typography.h6,
                    fontWeight = FontWeight.SemiBold,
                )
            }

但是,相同的代码对于材料设计3不起作用,并且文本具有默认的重点。另外,我找不到材料设计的任何地方的相关功能3。我想知道是否有任何官方方法可以达到相同的效果。

Currently, I am migrating one of my apps to Material Design 3 which is entirely written in Kotlin using Jetpack Compose.

While using Material Design 2, I was able to change the emphasis of the text using the code below.

CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
                Text(
                    text = "Hello, world",
                    style = MaterialTheme.typography.h6,
                    fontWeight = FontWeight.SemiBold,
                )
            }

However, the same code doesn't work for Material Design 3 and the text has the default emphasis. Also, I can't find the relevant function anywhere for Material Design 3. I would like to know if there is any official way to achieve the same effect.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

游魂 2025-02-13 17:08:49

材料thepograph.h6 来自材料2,这意味着您也使用 text 也可以从材料2中复合。

材料3 H6 的类似物IS 材料theme.typography.headlinesmall

确保您可以正确导入 text 材料 - 应从 androidx.compose.material3 软件包中导入这些。 Also make sure you provide a correctly imported theme, eg

错误的进口是迁移到M3时最常见的错误,因此请耐心等待。

另请注意, LocalContentalPha 在M3中不存在,提供M2版本对M3视图没有影响。您可以比较文本 composable在M2 and in

我不确定是否会在以后添加(毕竟是Alpha),或者在M3中以其他方式处理,这是一个解决方法(这不是一个完美的方法):

CompositionLocalProvider(LocalContentColor provides LocalContentColor.current.copy(alpha = 0.4f)) {

PS localContentColor 需求也要从M3进口

MaterialTheme.typography.h6 is from Material 2, which means you're using Text composable from Material 2 too.

Material 3 analog of h6 is MaterialTheme.typography.headlineSmall.

Make sure you have correct imports of both Text and MaterialTheme - these should be imported from androidx.compose.material3 package. Also make sure you provide a correctly imported theme, e.g. here.

Wrong imports is the most common mistake when migrating to M3, so be patient with it.

Also note that LocalContentAlpha doesn't exists in M3, providing M2 version will have no effect on M3 views. You can compare how Text composable determines its color in M2 and in M3.

I'm not sure wether it's gonna be added later (it's alpha after all), or it's handled in some other way in M3, here's a workaround(which is not perfect for sure):

CompositionLocalProvider(LocalContentColor provides LocalContentColor.current.copy(alpha = 0.4f)) {

p.s. LocalContentColor needs to be imported from M3 too

夜访吸血鬼 2025-02-13 17:08:49

材料3:不使用lotcontentalpha。相反,alpha已设置
直接在特定组件的颜色属性上。这
方法需要更明确的定义,但可以提供更明确的控制
在单个组件的样式上。

Text(
text = "Hello, world",
style = MaterialTheme.typography.headlineSmall.copy(
    fontWeight = FontWeight.SemiBold,
    color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f)
)
 )

Material 3: Does not use LocalContentAlpha. Instead, alpha is set
directly on the color property of the specific component. This
approach requires more explicit definition but offers clearer control
over the styling of individual components.

Text(
text = "Hello, world",
style = MaterialTheme.typography.headlineSmall.copy(
    fontWeight = FontWeight.SemiBold,
    color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f)
)
 )
沉默的熊 2025-02-13 17:08:48

强调和内容alpha =“ https://developer.android.com/jetpack/compose/compose/designsystems/material2-material3#m3_16” rel =“ noreferrer”>从材料2迁移到材料3到compose 详细信息的详细信息。

材料2:

import androidx.compose.material.ContentAlpha
import androidx.compose.material.LocalContentAlpha

// High emphasis
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.high) {
    Icon(…)
}
// Medium emphasis
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
    Icon(…)
}
// Disabled emphasis
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.disabled) {
    Icon(…)
}

材料3:

import androidx.compose.material3.LocalContentColor

// High emphasis
CompositionLocalProvider(LocalContentColor provides MaterialTheme.colorScheme.onSurface) {
    Icon(…)
}
// Medium emphasis
CompositionLocalProvider(LocalContentColor provides MaterialTheme.colorScheme.onSurfaceVariant) {
    Icon(…)
}
// Disabled emphasis
CompositionLocalProvider(LocalContentColor provides MaterialTheme.colorScheme.onSurface.copy(alpha = 0.38f)) {
    Icon(…)
}

The "Emphasis and content alpha" section in the Migrate from Material 2 to Material 3 in Compose details the API changes.

Material2:

import androidx.compose.material.ContentAlpha
import androidx.compose.material.LocalContentAlpha

// High emphasis
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.high) {
    Icon(…)
}
// Medium emphasis
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
    Icon(…)
}
// Disabled emphasis
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.disabled) {
    Icon(…)
}

Material3:

import androidx.compose.material3.LocalContentColor

// High emphasis
CompositionLocalProvider(LocalContentColor provides MaterialTheme.colorScheme.onSurface) {
    Icon(…)
}
// Medium emphasis
CompositionLocalProvider(LocalContentColor provides MaterialTheme.colorScheme.onSurfaceVariant) {
    Icon(…)
}
// Disabled emphasis
CompositionLocalProvider(LocalContentColor provides MaterialTheme.colorScheme.onSurface.copy(alpha = 0.38f)) {
    Icon(…)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文