Android 控件 - CardView(快速实现圆角)

CardView

1、基本介绍
  1. CardView 是 Android 在 androidx.cardview 包中提供的一个 UI 容器控件

  2. CardView 的核心作用是快速实现圆角

  3. CardView 还自带 Material Design 风格的阴影效果

2、CardView 属性
  1. 圆角相关
属性 说明
app:cardCornerRadius 设置圆角半径,单位 dp。值越大,圆角越圆
  1. 阴影相关
属性 说明
app:cardElevation 阴影高度,单位 dp。值越大,阴影越重
app:cardMaxElevation 最大阴影高度(一般不使用)
  1. 背景相关
属性 说明
app:cardBackgroundColor 卡片背景色,默认为白色
  1. 内边距相关
属性 说明
app:contentPadding 内边距
app:contentPaddingBottom 底部内边距
app:contentPaddingLeft 左侧内边距
app:contentPaddingRight 右侧内边距
app:contentPaddingTop 顶部内边距
3、演示
  1. 基本使用
xml 复制代码
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ViewNestActivity">

    <androidx.cardview.widget.CardView
        android:layout_width="400px"
        android:layout_height="300px"
        app:cardCornerRadius="10px"
        app:cardElevation="10px"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" />
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
  1. 去除阴影
xml 复制代码
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ViewNestActivity">

    <androidx.cardview.widget.CardView
        android:layout_width="400px"
        android:layout_height="300px"
        app:cardCornerRadius="10px"
        app:cardElevation="0px"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" />
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
  1. 使用背景与内边距
xml 复制代码
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ViewNestActivity">

    <androidx.cardview.widget.CardView
        android:layout_width="400px"
        android:layout_height="300px"
        app:cardCornerRadius="10px"
        app:cardElevation="10px"
        app:contentPaddingBottom="12dp"
        app:contentPaddingLeft="16dp"
        app:contentPaddingRight="16dp"
        app:contentPaddingTop="12dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/blue"
            android:orientation="vertical" />
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
  1. 画圆
xml 复制代码
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ViewNestActivity">

    <androidx.cardview.widget.CardView
        android:layout_width="400px"
        android:layout_height="400px"
        app:cardCornerRadius="200px"
        app:cardElevation="0px"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/blue"
            android:orientation="vertical" />
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
相关推荐
我是人✓1 分钟前
Spring IOC注解全解析和Spring整合测试单元
java·后端·spring
智购科技无人售货机工厂店8 分钟前
玻璃瓶饮料破损率突然上升,排查发现是取货口缓冲垫老化了~YH
java·开发语言·人工智能·python·eclipse
镜象科技16 分钟前
中小学AI心理健康解决方案服务商怎么选?2026年选型参考
android·java·人工智能
阿狗童鞋23 分钟前
高并发系统设计实战指南
java·spring boot·spring cloud
Sam_Deep_Thinking27 分钟前
CountDownLatch实现原理
java·后端·面试·程序员
脚踏实地,坚持不懈!28 分钟前
从 AOSP 源码角度分析 Android 定位性能问题
android
小狼Solar29 分钟前
SAP MDG 功能范围说明(基于S/4HANA 2025)
java·开发语言
范中勤30 分钟前
Python 与 Java:模块、包、对象、反射核心差异总结
java·python·面试·反射·元类
ab12376832 分钟前
Spring Integration 完整笔记 + 和 spring-integration-mqtt、Paho、EMQX 的关系
java·笔记·spring
小蜗牛狂飙记38 分钟前
java环境一键配置和切换的ui工具
java