我目前有一个用jetpack-compose编写的应用程序,它使用androidx.compose.material:material
的材料主题支持。
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
)
我现在应该如何处理旧的形状定义?材料网站只讨论如何用xml&旧的视图系统。
材料设计3/材料你仍然没有形状。因此,使用形状创建局部构图,
在目录ui/theme中创建Shape.ktKotlin文件,在该文件中粘贴以下代码
形状.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
) {
...
}
编辑:检查此