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>
相关推荐
学长毕设9 分钟前
基于SpringBoot的餐厅管理系统-附源码
java·spring boot·后端
拥小抱30 分钟前
Compose 知识点
android
刚入门的大一新生31 分钟前
Linux-线程互斥
java·开发语言
文章永久免费只为良心31 分钟前
Web应用安全与防护_第三章_日志文件包含_从一道 CTFshow 题目讲透「日志投毒型 LFI → RCE」:原理、时序陷阱与全变种攻防手册
android·网络安全·渗透测试·漏洞危害·漏洞描述·日志投毒·红队指南
AIsoft_86891 小时前
学术讲座录音如何整理?录音APP可行吗
android·iphone
赵小哥要冲啦1 小时前
社区邻里互助小程序从 0 到 1 复盘:需求拆解与信任机制设计
java·小程序·需求分析
xiaogai_gai1 小时前
采购业务退款单同步实战:从钉钉审批流到金蝶云星空付款单
java·数据库·钉钉
二哈赛车手1 小时前
新人笔记---hermes定时功能设计,具体讲解以及可优化点
java·人工智能·笔记·spring·ai·typescript
ZWZhangYu2 小时前
从 Dify 实践理解大模型上下文缓存
java·后端·spring
小羊没烦恼!2 小时前
关于分布式锁原理的一些学习与思考-redis分布式锁,zookeeper分布式锁
java·服务器·网络·学习·负载均衡