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

如何让Androidx.CardView在API 28及以上版本正常运行?

解决Androidx.CardView在API 28上的兼容问题

排查与修复步骤

1. 更新CardView依赖版本

旧版本的Androidx CardView可能存在API 28的适配bug,确保使用稳定适配的版本。在模块级build.gradle中更新依赖:

implementation "androidx.cardview:cardview:1.0.0" // 建议使用最新稳定版,1.0.0及以上已适配API 28

2. 修正圆角属性值

你的代码中app:cardCornerRadius="250dp"远大于CardView的宽高(50dp),高版本系统会自动处理为最大圆角,但API 28可能出现渲染异常。直接设置为宽高的一半,既保证圆形显示,又避免渲染问题:

<androidx.cardview.widget.CardView
    android:layout_width="50dp"
    android:layout_height="50dp"
    app:cardCornerRadius="25dp" <!-- 修改为宽高的一半 -->
    android:layout_marginEnd="40dp"
    app:cardBackgroundColor="@color/white">
    
    <ImageView
        android:id="@+id/google_sign_in_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="5dp"
        android:src="@drawable/icon_google" />
</androidx.cardview.widget.CardView>

3. 临时禁用硬件加速(应急方案)

API 28的硬件加速可能与CardView圆角渲染冲突,可针对该视图或所在Activity禁用:

  • 布局中给CardView添加属性:
android:hardwareAccelerated="false"
  • 或在Manifest的Activity标签中配置:
android:hardwareAccelerated="false"

注意:禁用硬件加速可能影响其他视图性能,仅作为临时排查手段,优先尝试前两种方案。

4. 检查颜色资源有效性

确保@color/white是合法的颜色资源,API 28对颜色解析更严格,避免使用无效的颜色值或引用路径。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 03:16:17