在 Jetpack Compose Glimmer 中,介面會使用形狀定義視覺邊界和圓角。Shapes 類別提供不同程度的圓角,適用於各種元件。

形狀類別
GlimmerTheme 類別定義了三種標準形狀大小:
- 小:四個角的大小相同,適用於資訊卡等元件。根據預設,這是 24.dp 的
RoundedCornerShape。 - 中:四個邊角大小相同,介於「小」和「大」之間。這是最常用的形狀,也是介面預設使用的形狀。根據預設,這是 36.dp 的
RoundedCornerShape。 - 大:四個角完全圓弧的形狀。這個形狀用於按鈕等元件。預設值為
CircleShape。
範例:從 GlimmerTheme 取得形狀並套用至元件
首先,從 GlimmerTheme.shapes 取得定義的形狀:
@Composable fun ShapesSample() { val shapes = GlimmerTheme.shapes GlimmerLazyColumn { item { ShapeItem("small", shape = shapes.small) } item { ShapeItem("medium", shape = shapes.medium) } item { ShapeItem("large", shape = shapes.large) } } }
接著,您可以將這些形狀套用至部分元件:
@Composable private fun ShapeItem(name: String, shape: Shape, modifier: Modifier = Modifier) { Box( modifier.aspectRatio(2.5f).fillMaxWidth().surface(shape = shape), contentAlignment = Alignment.Center, ) { Text(name) } }
自訂形狀
Shapes 類別為 @Immutable。您可以建立現有主題形狀的副本,並使用複製函式覆寫特定值。這麼做可維持主題的結構,同時調整應用程式品牌的特定半徑。
範例:覆寫特定形狀值
下列程式碼會覆寫特定形狀值:
val customShapes = GlimmerTheme.shapes.copy(
small = RoundedCornerShape(12.dp),
medium = RoundedCornerShape(20.dp)
)
預設值
如未在 GlimmerTheme 中另行指定,系統會預設為下列值:
| 權杖 | 預設形狀 | 大小或半徑 |
|---|---|---|
|
|
24.dp |
|
|
36.dp |
|
|
Fully Rounded |