material-design - 为 Compose 创建 Material Design 3 主题时如何处理形状

标签 material-design android-jetpack-compose material-components-android android-jetpack-compose-material3

我目前有一个用 jetpack compose 编写的应用程序,它使用来自 androidx.compose.material:material 的 Material-Theming-Support。

from / import androidx.compose.material.MaterialTheme

@Composable
fun MaterialTheme(
    colors: Colors = MaterialTheme.colors,
    typography: Typography = MaterialTheme.typography,
    shapes: Shapes = MaterialTheme.shapes,
    content: @Composable () -> Unit
)

我现在计划迁移到 Material3:androidx.compose.material3:material3(我知道仍处于 alpha 阶段)。

但是,主题可组合项现在不再允许任何形状

from / import androidx.compose.material3.MaterialTheme

@Composable
fun MaterialTheme(
    colorScheme: ColorScheme = MaterialTheme.colorScheme,
    typography: Typography = MaterialTheme.typography,
    content: @Composable () -> Unit
)

我现在应该如何处理我的旧形状定义? Material 网站只讨论如何在 xml 和 old view-system 中做到这一点.

最佳答案

Material Design 3 / Material You still don't have shapes. 所以要使用形状创建 composition local ,

在目录 ui/theme 中创建 Shape.kt Kotlin 文件,在该文件中粘贴以下代码

Shape.kt

data class Shape(
    val default: RoundedCornerShape = RoundedCornerShape(0.dp),
    val small: RoundedCornerShape = RoundedCornerShape(4.dp),
    val medium: RoundedCornerShape = RoundedCornerShape(8.dp),
    val large: RoundedCornerShape = RoundedCornerShape(16.dp)
)

val LocalShape = compositionLocalOf { Shape() }

val MaterialTheme.shapeScheme: Shape
    @Composable
    @ReadOnlyComposable
    get() = LocalShape.current

现在,在撰写中使用形状

shape = MaterialTheme.shapeScheme.large

例如。在卡片中撰写

Card(
        modifier = Modifier
            .fillMaxWidth()
            .wrapContentHeight()
            .padding(all = 16.dp),
        shape = MaterialTheme.shapeScheme.large,
        backgroundColor = MaterialTheme.colorScheme.surface,
        border = BorderStroke(width = 1.dp, color = MaterialTheme.colorScheme.inverseOnSurface),
        elevation = 1.dp
    ) {
...
}

编辑: Check this

关于material-design - 为 Compose 创建 Material Design 3 主题时如何处理形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70668348/

相关文章:

android - 如何在 Jetpack Compose 中实现这种布局

java - 使用material.textfield.TextInputLayout时,我的轮廓框覆盖了提示。如何在不覆盖盒子的情况下显示提示?

android - 如何在扩展 float 操作按钮中居中对齐文本?

android - InputTextLayout,将错误文本设置为帮助文本(不同颜色)

javascript - 使用 CSS API 覆盖 Material UI Stepper 中的 CSS 样式

android - MaterialComponents AlertDialog 文本颜色

android - 使用导航组件设置 BottomNavView 后,Jetpack Compose TopAppBar 闪烁

android - Jetpack Compose 用户界面 : How to create SearchView?

android - TextInputLayout boxBackgroundColor 在 'com.google.android.material:material:1.1.0-alpha02' 之后不起作用

html - 为外部链接实现标签