You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jetpack Glance中为Row/Column设置圆角?兼容低版本Android

Jetpack Glance 低版本兼容的圆角布局实现方案

Glance自带的cornerRadius修饰符仅在Android S(API 31)及以上版本生效,要兼容更低版本,可通过自定义Shape Drawable作为背景的方式实现圆角效果,具体步骤如下:

1. 创建带圆角的Shape Drawable

在res/drawable目录下新建rounded_background.xml文件,定义带圆角的白色背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/white" />
    <corners android:radius="100dp" /> <!-- 与原代码中的圆角半径保持一致 -->
</shape>

2. 修改Glance Composable代码

将原代码中background(Color.White) + cornerRadius(100.dp)的组合,替换为引用上述drawable的background修饰符:

@Composable
fun ParkingSessionsWidget(sessions: List<ParkingSession>) {
    Column(
        modifier = GlanceModifier.fillMaxWidth()
            .height(100.dp)
            .padding(16.dp)
            .background(R.drawable.rounded_background) // 替换为自定义drawable
    ) {
        Text(
            text = "Active sessions",
            style = defaultTextStyle.copy(fontSize = 20.sp, fontWeight = FontWeight.Bold),
            modifier = GlanceModifier.padding(16.dp)
        )
    }
}

补充说明

该方案通过系统原生Drawable实现圆角效果,兼容Glance支持的所有最低版本(API 21+),视觉表现与高版本的cornerRadius修饰符完全一致,同时保持代码简洁易维护。

内容的提问来源于stack exchange,提问作者JEssicDo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 06:53:13