[关闭]
@946898963 2018-09-03T07:50:30.000000Z 字数 8488 阅读 1278

CardView

Android学习笔记


本文谨供个人学习使用,文章内容多摘自参考链接中的文章。

CardView卡片视图

Android L(Android 5.0)开始,Google给开发者带来了一个可实现立体感的控件-CardView,从本质上看,CardView是在FrameLayout的基础上添加了圆角和阴影效果。CardView作为一种容器,并且经常在ListView和RecyclerView的Item布局中,可使item都有圆角及阴影的效果,满足MD设计规范。在开发中如果需要实现阴影,圆角,3D什么的效果,可以试试cardview,当然通过layerlist结合selector也可以实现很好的交互。

说到底,CardView也是一个容器类布局,只是它提供了卡片这样一种形式。开发者可以定义卡片的大小与视图高度,并设置圆角的角度。

在高版本中使用CardView是很舒服的,很容易实现各种效果,但是在低版本上兼容性不是很好。

继承

此处输入图片的描述

属性

  1. app:cardBackgroundColor-- 背景色
  2. app:cardCornerRadius-- 边缘弧度数
  3. app:cardElevation-- 高度
  4. app:cardMaxElevation-- 最大高度
  5. app:cardUseCompatPadding-- 设置内边距,v21+的版本和之前的版本仍旧具有一样的计算方式
  6. app:cardPreventCornerOverlap-- v20和之前的版本中添加内边距,这个属性是为了防止卡片内容和边角的重叠
  7. app:contentPadding-- 卡片边界距离内部的距离
  8. app:contentPaddingLeft-- 卡片边界距离左边的距离
  9. app:contentPaddingTop-- 卡片边界距离顶边的距离
  10. app:contentPaddingRight-- 卡片边界距离右边的距离
  11. app:contentPaddingBottom-- 卡片边界距离底边的距离

此处输入图片的描述

1、设置背景颜色
app:cardBackgroundColor=" "

2、设置padding
app:contentPadding=" "app:contentPaddingTop=" "app:contentPaddingBottom=" "app:contentPaddingLeft=" "app:contentPaddingRight=" "

Tips:上面是CardView设置背景颜色和padding的方式,如果你直接通过android:padding=" " 和android:background=" "设置,是无效的.

3、设置Z轴的最大高度
app:cardMaxElevation=" "

4、点击之后的涟漪效果
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
Tips:如果你给CardView设置了点击事件,就不需要设置android:clickable="true"了

如果你的CardView是可点击的,可以通过foreground属性使用系统定义好的RippleDrawable: selectableItemBackground,从而达到在5.0及以上版本系统中实现点击时的涟漪效果(Ripple)。

这个涟漪效果在5.0以上版本中才能展示,在低版本上是一个普通的点击变暗的效果.

关于Z轴的概念

Android5.0 引入了Z轴的概念,可以让组件呈现3D效果。看下面这幅图:

此处输入图片的描述

图中的FAB(FloatingActionButton)很明显是浮在界面上的,这就是Z轴的效果。Z属性可以通过elevation和translationZ进行修改Z= elevation+translationZ
android:elevation=" " 设置该属性使控件有一个阴影,感觉该控件像是“浮”起来一样,达到3D效果

android:translationZ="" 设置该组件阴影在Z轴(垂直屏幕方向)上的位移

5.0之前,我们如果想给View添加阴影效果,以体现其层次感,通常的做法是给View设置一个带阴影的背景图片。在5.0之后,我们只需要简单的修改View的Z属性,就能让其具备阴影的层次感,不过要求版本至少5.0 Lollipop,也就是API21。

Android Design Support Librarysupport-v7中一些组件已经封装好了Z属性,不需要5.0 就可以使用。

FloatingActionButton就可以通过app:elevation=""使用Z属性,CardView可以通过app:cardElevation=" " 来使用。关于Z轴的更多介绍,可以观看官方:定义阴影与裁剪视图

基本使用

1.在应用的build.gradle中添加支持库,版本就看自己的了

  1. compile 'com.android.support:support-v4:25.3.1'

2.布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:gravity="center_horizontal"
  9. android:orientation="vertical"
  10. tools:context="com.materialdesignui.cardview.CardViewActivity">
  11. <android.support.v7.widget.CardView
  12. android:layout_marginTop="20dp"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content">
  15. <TextView
  16. android:text="普通狀態cardview"
  17. android:gravity="center"
  18. android:padding="5dp"
  19. android:layout_width="wrap_content"
  20. android:layout_height="25dp"/>
  21. </android.support.v7.widget.CardView>
  22. <android.support.v7.widget.CardView
  23. android:layout_marginTop="20dp"
  24. app:cardCornerRadius="7dp"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content">
  27. <TextView
  28. android:text="圆角狀態cardview"
  29. android:gravity="center"
  30. android:padding="5dp"
  31. android:layout_width="wrap_content"
  32. android:layout_height="25dp"/>
  33. </android.support.v7.widget.CardView>
  34. <android.support.v7.widget.CardView
  35. android:layout_marginTop="20dp"
  36. app:cardElevation="6dp"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content">
  39. <TextView
  40. android:text="阴影狀態cardview"
  41. android:gravity="center"
  42. android:padding="5dp"
  43. android:layout_width="wrap_content"
  44. android:layout_height="25dp"/>
  45. </android.support.v7.widget.CardView>
  46. <android.support.v7.widget.CardView
  47. android:layout_marginTop="20dp"
  48. app:contentPadding="15dp"
  49. app:cardBackgroundColor="@android:color/holo_orange_light"
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content">
  52. <TextView
  53. android:text="内边距狀態cardview"
  54. android:background="@android:color/white"
  55. android:gravity="center"
  56. android:padding="5dp"
  57. android:layout_width="wrap_content"
  58. android:layout_height="25dp"/>
  59. </android.support.v7.widget.CardView>
  60. <android.support.v7.widget.CardView
  61. android:layout_marginTop="20dp"
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content">
  64. <ImageView
  65. android:src="@drawable/tab_01"
  66. android:layout_width="100dp"
  67. android:layout_height="100dp"/>
  68. </android.support.v7.widget.CardView>
  69. <android.support.v7.widget.CardView
  70. android:layout_marginTop="20dp"
  71. android:clickable="true"
  72. app:cardElevation="10dp"
  73. app:cardCornerRadius="10dp"
  74. android:foreground="?attr/selectableItemBackground"
  75. android:layout_width="wrap_content"
  76. android:layout_height="wrap_content">
  77. <ImageView
  78. android:src="@drawable/tab_01"
  79. android:layout_width="100dp"
  80. android:layout_height="100dp"/>
  81. </android.support.v7.widget.CardView>
  82. </LinearLayout>

3.效果如下:
此处输入图片的描述

这里需要说下,点击cardview所产生的水波纹效果,CardView 加上android:foreground=”?attr/selectableItemBackground” 这个属性会在 Lollipop 上自动加上 Ripple 效果,在旧版本则是一个变深/变亮的效果。

结合recyclerview

效果,单击也设置了水波纹的效果

此处输入图片的描述

布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"
  7. android:orientation="vertical">
  8. <android.support.v7.widget.CardView
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_margin="15dp"
  12. android:clickable="true"
  13. android:foreground="?attr/selectableItemBackground"
  14. app:cardCornerRadius="10dp"
  15. app:cardElevation="15dp">
  16. <RelativeLayout
  17. android:layout_width="match_parent"
  18. android:layout_height="match_parent">
  19. <ImageView
  20. android:id="@+id/iv"
  21. android:src="@drawable/tab_03"
  22. android:layout_width="108dp"
  23. android:layout_height="108dp"
  24. android:padding="12dp"
  25. android:scaleType="centerCrop"/>
  26. <TextView
  27. android:id="@+id/tv_card"
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:layout_centerVertical="true"
  31. android:layout_marginLeft="36dp"
  32. android:layout_toRightOf="@id/iv"
  33. android:gravity="center"
  34. android:padding="3dp"
  35. android:text=""/>
  36. </RelativeLayout>
  37. </android.support.v7.widget.CardView>
  38. </LinearLayout>

activity:

  1. public class CardRecyclerActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_card_recycler);
  6. RecyclerView mRv = (RecyclerView) findViewById(R.id.rv_card);
  7. mRv.setLayoutManager(new LinearLayoutManager(this));
  8. mRv.setAdapter(new CardRecAdapter());
  9. }
  10. }

adapter:

  1. public class CardRecAdapter extends RecyclerView.Adapter<CardRecAdapter.ViewHolder> {
  2. private List<String> mList;
  3. public CardRecAdapter() {
  4. this.mList = new ArrayList<>();
  5. for (int i = 1; i <= 60; i++) {
  6. mList.add("测试中的数据。。。" + i);
  7. }
  8. }
  9. @Override
  10. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  11. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_card, parent, false);
  12. return new ViewHolder(view);
  13. }
  14. @Override
  15. public void onBindViewHolder(ViewHolder holder, int position) {
  16. holder.render(mList.get(position));
  17. }
  18. @Override
  19. public int getItemCount() {
  20. return mList.size();
  21. }
  22. public class ViewHolder extends RecyclerView.ViewHolder{
  23. private TextView mTv;
  24. public ViewHolder(View itemView) {
  25. super(itemView);
  26. mTv = (TextView) itemView.findViewById(R.id.tv_card);
  27. }
  28. public void render(String text){
  29. mTv.setText(text);
  30. }
  31. }
  32. }

注意点

  1. CardView从本质上属于FrameLayout,而CardView通常包含了较多的内容元素,为了方便地排版布局中的各个元素,一般借助于其他基本布局容器,比如这里我们使用了一个 RelativeLayout 作为CardView的唯一Child。

  2. 在Android 5.0之前的版本中设置了app:cardElevation=""后CardView会自动留出空间供阴影显示,而5.0之后的版本中没有预留空间。这也导致了以Lollipop为分界线的不同系统上CardView的尺寸大小不同。所以给CardView设置 Margin时需要兼容一下,否则在低版本上每个卡片之间的距离会特别大,浪费屏幕空间.
    此处输入图片的描述

为了解决这个问题,有两种方法:

第一种,使用不同API版本的dimension资源适配(也就是借助values和values-21文件夹中不同的dimens.xml文件);

  1. 1.创建 /res/value /res/value-v21 资源文件夹于项目对应 Module 目录下,前者放置旧版本/通用的资源文件(了解的可以跳过),后者放置 21 及更高 SDK 版本的资源文件。
  2. 2. value 内的 dimen.xml 创建一个 Dimension (<dimen> 属性),随便命个名(如 xxx_card_margin)并填入数值 0dp
  3. 3.接着在 value-v21 文件夹内的 dimen.xml 创建名字相同的 Dimension,并填入你期望的预留边距(一般和 CardElevation 阴影大小相同)
  4. 4.最后,在你布局中的 CardView 中设置 android:layout_margin="@dimen/card_margin"

第二种,就是使用 setUseCompatPadding属性,设置为true(默认值为false),让CardView在不同系统中使用相同的padding值。

3.实现水波纹的动画选择器,创建一个 TranslationZ的变换动画放在/res/anim,自己取一个名(如 touch_raise.xml),加入以下内容:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_enabled="true" android:state_pressed="true">
  4. <objectAnimator
  5. android:duration="@android:integer/config_shortAnimTime"
  6. android:propertyName="translationZ"
  7. android:valueTo="@dimen/touch_raise"
  8. android:valueType="floatType" />
  9. </item>
  10. <item>
  11. <objectAnimator
  12. android:duration="@android:integer/config_shortAnimTime"
  13. android:propertyName="translationZ"
  14. android:valueTo="0dp"
  15. android:valueType="floatType" />
  16. </item>
  17. </selector>
  18. android:stateListAnimator="@anim/touch_raise"。

参考链接:

CardView使用详解

CardView的使用(关于文中的圆角效果没看懂)

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注