CardView是在Android 5.0推出的新控件,为了兼容之前的版本,将其放在了v7包里面,在现在扁平化设计潮流的驱使下,越来越多的软件使用到了CardView这一控件,那么这篇文章就来看看CardView怎么使用吧

CardView的特有属性

cardBackgroundColor 背景色

cardCornerRadius 边缘弧度数

cardElevation 高度

cardMaxElevation 最大高度

cardUseCompatPadding 设置内边距

cardPreventCornerOverlap 防止卡片内容和边角的重叠

contentPadding 内边距

contentPaddingLeft 内左边距

contentPaddingRight 内右边距

contentPaddingTop 内上边距

contentPaddingBottom 内下边距

CardView使用

由于CardView的使用比较简单,这里不再过多描述

使用前导入依赖

implementation 'com.android.support:cardview-v7:25.4.0'

文字

<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity"> <android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="200dp"
app:cardBackgroundColor="@android:color/holo_green_light"
app:cardCornerRadius="8dp"
app:cardElevation="10dp"
app:contentPadding="20dp"
android:layout_margin="0dp"> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:text="card view text"
android:textColor="@android:color/white" />
</android.support.v7.widget.CardView> </LinearLayout>

效果如下

图片

<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity"> <android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp"
app:cardElevation="10dp"
app:contentPadding="20dp"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher" />
</android.support.v7.widget.CardView> </LinearLayout>

效果如下

适配注意点

  1. 边框圆角效果

    5.x 图片和布局都可以很好的呈现圆角效果,图片也变圆角了

    4.x 图不能变成圆角,如果要做成5.x一样的效果:通过加载图片的时候自己去处理成圆角
  2. 阴影效果
app:cardCornerRadius="10dp"<!--圆角(半径值越大圆角就越明显)-->
app:cardElevation="10dp" <!--阴影效果(值越大阴影效果越明显)-->
  1. 5.x上有Ripple水波纹效果(低版本自定义)
android:foreground="?attr/selectableItemBackground"
android:clickable="true"
  1. 5.x实现按下的互动效果下沉,松开弹起 - Z轴位移效果(低版本自定义)
android:stateListAnimator="@drawable/translation"
android:clickable="true"
  1. 可以设置内容的内边距
app:contentPadding="5dp"

细节:

5.x上面,边距阴影比较小,需要手动添加边距16dp

4.x上面,边距太大, 手动修改边距0dp(原因:兼容包里面设置阴影效果自动设置了margin来处理16dp)

translation.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="10dp"
android:valueType="floatType" />
</item> <item>
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="0dp"
android:valueType="floatType" />
</item>
</selector>

高级UI-CardView的更多相关文章

  1. firefox 扩展开发笔记(三):高级ui交互编程

    firefox 扩展开发笔记(三):高级ui交互编程 前言 前两篇链接 1:firefox 扩展开发笔记(一):jpm 使用实践以及调试 2:firefox 扩展开发笔记(二):进阶开发之移动设备模拟 ...

  2. Android 高级UI设计笔记07:RecyclerView 的详解

    1. 使用RecyclerView       在 Android 应用程序中列表是一个非常重要的控件,适用场合非常多,如新闻列表.应用列表.消息列表等等,但是从Android 一出生到现在并没有非常 ...

  3. iOS开发——高级UI&带你玩转UITableView

    带你玩装UITableView 在实际iOS开发中UITableView是使用最多,也是最重要的一个控件,如果你不会用它,那别说什么大神了,菜鸟都不如. 其实关于UItableView事非常简单的,实 ...

  4. 高级UI晋升之自定义View实战(六)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从Android 自定义属性动画&Camera动画来介绍自定义V ...

  5. 高级UI晋升之布局ViewGroup(四)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从LinearLayout.RelativeLayout.FrameLa ...

  6. 高级UI晋升之常用View(三)中篇

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从ViewPager来介绍常用View:文章目录 一.简介 二.基本使用 ...

  7. 高级UI晋升之常用View(三)上篇

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将先从以下两个内容来介绍常用View: [RecycleView] [Ca ...

  8. 高级UI晋升之View渲染机制(二)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 优化性能一般从渲染,运算与内存,电量三个方面进行,今天开始说聊一聊Android ...

  9. 高级UI晋升之触摸事件分发机制(一)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 0. 前言 鉴于安卓分发机制较为复杂,故分为多个层次进行讲解,分别为基础篇.实践 ...

  10. Android 高级UI设计笔记21:Android SegmentView(分段选择控件)

    1. 分段控制(SegmentView) 首先我们先看看什么是SegmentView的效果,如下: 分段控制这个View控件是ios7的分段控制,和QQ消息页面顶部的效果一样,android没有这个控 ...

随机推荐

  1. RookeyFrame Bug 表单管理 -> 查看表单 ->编辑字段页面 JS报错

    表单管理 -> 查看表单 ->编辑字段页面 小bug onchange里面直接就是方法,修改:去掉外面的function(){},直接把方法体写在onchange里面就可以了. 后台方法: ...

  2. Ural1297 最长回文子串(后缀数组+RMQ)

    /* 源程序丢失QWQ. 就不粘代码了. 大体做法是把串反转然后连接. 做一遍后缀数组. 对height做一遍rmq. 然后对于每个位置的奇偶分别判断, 记下pos. 注意求的是[l+1,r]的hei ...

  3. Java SpringBoot Scheduled定时任务

    package task.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import ...

  4. c标签简单应用

        <pager:column  property="ly" title="任务类型" width="10%">       ...

  5. VsCode插件与Node.js交互通信

    首先关于VsCode插件通信,如果不明白的可以参考我的这篇博客VsCode插件开发之插件初步通信 如果需要详细例子的话,可以参考VsCode插件开发 现在又有一个新的需求是,VsCode插件可以通过j ...

  6. Eclipse/STS选择一段文本进行复制变得很卡的解决方案

    网上给出的解决方案是: 更改打开代码超链接按键Ctrl为Alt:Window -> Preferences -> General -> Editors -> Text Edit ...

  7. flutter State管理

    import 'package:flutter/material.dart'; import 'package:scoped_model/scoped_model.dart'; class State ...

  8. 16个python常用魔法函数

    ==,is的使用 ·is是比较两个引用是否指向了同一个对象(引用比较). ·==是比较两个对象是否相等 1.__ init__(): 所有类的超类object,有一个默认包含pass的__ init ...

  9. openresty开发系列24--openresty中lua的引入及使用

    openresty开发系列24--openresty中lua的引入及使用 openresty 引入 lua 一)openresty中nginx引入lua方式 1)xxx_by_lua   ---> ...

  10. openresty开发系列15--lua基础语法4表table和运算符

    openresty开发系列15--lua基础语法4表table和运算符 lua中的表table 一)table (表)Table 类型实现了一种抽象的"关联数组".即可用作数组,也 ...