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>
相关推荐
wno70420 分钟前
Spring Security权限控制
java·python·spring
杨运交1 小时前
[071][验证码模块]基于Spring拦截器的验证码认证设计思想
java·后端·spring
Geek-Chow1 小时前
CountDownLatch in Java
java
SL_staff2 小时前
从RBAC到场景化授权:《无忧·企业文档》三级权限模型的技术实践解析
java·开源·产品
传奇开心果编程2 小时前
【springboot基础语法学与练】第 1 课:从零开始
java·spring boot·后端·学习
SL_staff2 小时前
财务系统慎用低代码?从数据模型闭环看合规落地的技术实践
java·低代码·全栈
滕州市燕猫虎计算机科技工作室个体工商户3 小时前
IDEA:Command line is too long
java·ide·intellij-idea
BoomHe3 小时前
Android Framework 文件应用移植到 AndroidStudio
android
步行cgn3 小时前
Spring p 命名空间注入详解
java·前端·spring
YatHinLay3 小时前
MyBatis 流式查询实战:ResultHandler 处理海量数据
java