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>
相关推荐
吴声子夜歌3 分钟前
Java——基本类型
java·开发语言
用户693717500138419 分钟前
曾经安卓开发人手一个的 EventBus,为什么现在没人爱用了?
android·android studio
CoderYanger2 小时前
A.每日一题:输入单词需要的最少按键次数 Ⅰ+Ⅱ
java·数据结构·算法·leetcode·面试
Cosolar2 小时前
一文弄懂 Agent Harness 与 Agent Runtime 的区别
java·后端·github
吴声子夜歌2 小时前
Java——类、对象及方法(一)
java·开发语言
程序员夏洛2 小时前
Redis 的持久化机制有哪些?
java·数据库·redis
九皇叔叔2 小时前
MySQL ORDER BY 性能优化详解:Using filesort、联合索引、ASC/DESC 与覆盖索引
android·adb
凯尔萨厮2 小时前
Java学习笔记十(注解)
java·笔记·学习
钱栈up2 小时前
VSCode 调试多模块 Maven + Spring Boot 后端:从启动失败到一次跑通
java·spring boot·maven
uoKent2 小时前
c++中new和malloc的区别
java·jvm·c++