当前位置: 首页 > article >正文

Android CardView基础使用

目录

一、CardView

1.1 导入material库

1.2 属性

二、使用(效果)

2.1 圆角卡片效果

2.2 阴影卡片效果

2.3 背景

        2.3.1 设置卡片背景(app:cardBackgroundColor)

        2.3.2 内嵌布局,给布局设置背景色

2.4 进阶版

        2.4.1 带透明度

        2.4.2 无透明度


一、CardView

        顾名思义,就是卡片布局。现在卡片布局越来越普及了,以前都是各种shape来实现卡片效果,现在可以直接用控件来实现,是不是美滋滋?CardView实用性还是比较强的,一起来试试吧?

1.1 导入material库

implementation 'com.google.android.material:material:1.10.0'
material:1.10.0 要求SDK大于等于34,如果不打算兼容那么高的,建议降低至 1.8.0 ,不影响 CardView 的使用。

        CardView继承自FrameLayout,可以让我们使用类似卡片布局来显示一致性效果的内容。同时卡片还可以包含圆角和阴影效果。

1.2 属性

  • app:cardBackgroundColor 设置背景颜色

  • app:cardCornerRadius 设置圆角大小,不要圆角设置为0

  • app:cardElevation 设置z轴的阴影,不需要阴影设置为0

  • app:cardMaxElevation 设置z轴的最大高度值

  • app:contentPadding 设置padding

  • app:contentPaddingLeft

  • app:contentPaddingTop

  • app:contentPaddingRight

  • app:contentPaddingBottom

需要注意的是这里的前缀是 app ,而不是 android

二、使用(效果)

2.1 圆角卡片效果

        默认是带点阴影效果的。

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:background="@color/purple_200"
        app:cardCornerRadius="10dp"
        app:contentPadding="20dp"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:text="圆角 CardView "
            android:textColor="@color/black"
            android:layout_height="wrap_content"/>
    </androidx.cardview.widget.CardView>

2.2 阴影卡片效果

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        app:cardCornerRadius="10dp"
        app:cardElevation="20dp"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="20dp"
            android:text="阴影 CardView"
            android:textColor="@color/black"
            android:layout_height="wrap_content"/>
    </androidx.cardview.widget.CardView>

2.3 背景

        2.3.1 设置卡片背景(app:cardBackgroundColor)

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        app:cardBackgroundColor="@color/purple_200"
        app:cardCornerRadius="10dp"
        app:cardElevation="20dp"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content">
            <TextView
                .../>
            <TextView
                .../>
        </LinearLayout>
    </androidx.cardview.widget.CardView>

        2.3.2 内嵌布局,给布局设置背景色

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        app:cardCornerRadius="10dp"
        app:cardElevation="20dp"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:background="@color/purple_200"
            android:layout_height="wrap_content">
            <TextView
                .../>
            <TextView
                .../>
        </LinearLayout>
    </androidx.cardview.widget.CardView>

2.4 进阶版

        同时设置 app:cardBackgroundColorandroid:background

        2.4.1 带透明度

  <androidx.cardview.widget.CardView
        app:cardBackgroundColor="#44ff0000"
        android:background="#440000ff"
        ...>
        <TextView
            android:background="#440000ff"
            />
    </androidx.cardview.widget.CardView>

        2.4.2 无透明度

    <androidx.cardview.widget.CardView
        ...
        app:cardBackgroundColor="#ff0000"
        android:background="#00ff00">
        <TextView
            ...
            android:background="#0000ff"/>
    </androidx.cardview.widget.CardView>
android:background明显会被app:cardBackgroundColor消费(覆盖)掉

http://www.kler.cn/news/156005.html

相关文章:

  • 反序列化漏洞详解(三)
  • blender导出相机参数
  • ESP32使用mpu6050以及pid调参
  • Python - 字典3
  • 详解云WAF:免费GOODWAF归来
  • HarmonyOS—ArkTS中@Observed和@ObjectLink装饰器的嵌套类对象属性变化【鸿蒙专栏-11】
  • C语言实现Cohen_Sutherland算法
  • 构造函数的定义
  • Prometheus+Grafana搭建日志采集
  • Langchain-Chatchat的安装过程
  • C++基础 -35- string类
  • 操作系统·存储器管理
  • 亚信科技AntDB数据库完成中国信通院数据库迁移工具专项测试
  • unordered_map与unordered_set的实现(含迭代器)
  • go使用aes加密算法
  • 【ArcGIS Pro微课1000例】0043:深度学习--框架库安装
  • 【java+vue+微信小程序项目】从零开始搭建——健身房管理平台(3)路由导航卫士、主页实现
  • Jupyter NoteBook未授权访问漏洞
  • 2024年天津财经大学珠江学院专升本专业课报名缴费操作流程
  • KALI LINUX入门
  • LC-1423. 可获得的最大点数(子数组和、枚举)
  • 牛客在线编程(SQL大厂面试真题)
  • 抓取检测(Grasp Dection)
  • U盘不仅能在电脑上使用,在手机上也可使用,包括安卓和苹果手机,但苹果的较特殊
  • 二叉树k层的叶子结点个数
  • 【Qt开发流程】之自定义语法高亮和使用HTML语法
  • Proteus仿真--基于ADC0832设计的两路电压表
  • Mongodb安装及其使用
  • Linux 基本语句_14_信号灯实验
  • Hdoop学习笔记(HDP)-Part.06 安装OracleJDK