在 Compose 中将 XML 主题迁移到 Material 3 和 compose的一点记的东西

查看原有的xml
根据xml原有的信息确定形状大小等等
字符串等资源可按原样重复使用,无需迁移

迁移配色(例)

<shape android:shape="rectangle">
    <corners android:radius="16dp" />
</shape>
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Shapes
import androidx.compose.ui.unit.dp

val AppShapes = Shapes(
    // Material 3 将形状分为 5 个档位,可以根据组件大小选择
    extraSmall = RoundedCornerShape(4.dp),
    small = RoundedCornerShape(8.dp),
    medium = RoundedCornerShape(12.dp), 
    large = RoundedCornerShape(16.dp),  // 对应 XML 中的 16dp
    extraLarge = RoundedCornerShape(28.dp)
)

//挡位让各个不同地方的圆角大小不同,好看。。
这一段写在theme.kt里面
emmm,官方也只说了这一点,我也只能想到补充这一点

那就把compose的一部分东西写这里好了

@Composable
fun GreetingImage(
   message: String, 
   from: String, 
   modifier: Modifier = Modifier // = 代表可选项
)

当没有传入(没有写)会采用默认选项(默认值)
Box之类的有类似的行为
比如写Box写空的,甚至误写成Modifier或者modifier = Modifier
会自动生成一个空的对象
然后就会警告()形参“modifier” 从未使用

posted @ 2026-03-20 00:43  气温骤降  阅读(3)  评论(0)    收藏  举报