背景、边框与裁剪
添加颜色、轮廓和圆角形状。
背景、边框与裁剪 是 CoddyKit 上的免费 Jetpack Compose Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Jetpack Compose Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Jetpack Compose Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Decorate with Modifiers
Beyond sizing, modifiers can decorate a Composable. You can paint a background, draw a border, and round its corners. 🎨
Add a Background
background fills a Composable with a solid color. It paints the whole area the element occupies, including its size and fill.
Modifier.background(Color.Blue)Use Theme Colors
Prefer theme colors over raw values. Pulling from MaterialTheme keeps your app consistent in light and dark mode.
Modifier.background(
MaterialTheme.colorScheme.primary
)Draw a Border
Use border to outline a Composable. Give it a thickness in dp and a color, and Compose draws the edge for you.
Modifier.border(2.dp, Color.Gray)Meet Shapes
A Shape describes an outline like rounded or circular. Both background and border accept a shape to change their silhouette.
Rounded Corners
RoundedCornerShape softens corners by a radius in dp. It is the go-to shape for cards, buttons, and chips.
RoundedCornerShape(12.dp)Shaped Background
Pass a shape to background and the color fills only inside that outline, giving you a tidy rounded panel.
Modifier.background(
Color.LightGray,
RoundedCornerShape(12.dp)
)Clip the Content
clip trims anything drawn outside a shape. Use it so images and children respect your rounded corners.
Modifier.clip(RoundedCornerShape(12.dp))Make a Circle
Reach for CircleShape when you want a perfect circle, like a round avatar or a floating button.
Modifier.clip(CircleShape)Clip Before Drawing
Place clip before background so the fill is also rounded. Order in the chain decides what gets clipped.
Modifier
.clip(RoundedCornerShape(12.dp))
.background(Color.Cyan)Combine for a Card
Stack clip, background, and border together to craft a polished card surface in just a few lines.
Modifier
.clip(RoundedCornerShape(16.dp))
.background(Color.White)
.border(1.dp, Color.LightGray)Quick Check
You want a rounded background where the fill itself is also rounded. Which modifier ensures that?
Recap
You can now style any element: background paints it, border outlines it, and clip with a Shape rounds it. 🎉
常见问题解答
「背景、边框与裁剪」课时是免费的吗?
是的 — 「背景、边框与裁剪」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Jetpack Compose Academy 课程的其余内容,请升级到 CoddyKit PRO。 Jetpack Compose Academy 课程共包含 4 节课。
「背景、边框与裁剪」这节课中我会学到什么?
添加颜色、轮廓和圆角形状。 你通过在浏览器中直接运行的动手代码来练习 Jetpack Compose Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Jetpack Compose Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Jetpack Compose Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「背景、边框与裁剪」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Jetpack Compose Academy 课中编写并运行代码吗?
能。每节 Jetpack Compose Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。