引用:compile
'com.android.support:cardview-v7:21.0.+'
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="200dp"
android:layout_height="200dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/cheese_1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="45dp"
android:layout_marginTop="10dp"
app:cardBackgroundColor="@color/colorAccent"
app:cardCornerRadius="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
android:gravity="center"
android:text="圆角 cardCornerRadius=20dp"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="45dp"
android:layout_marginTop="10dp"
app:cardBackgroundColor="@color/colorPrimary"
app:cardCornerRadius="20dp"
app:cardElevation="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:gravity="center"
android:text="背景颜色 cardBackgroundColor"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="45dp"
android:layout_marginTop="10dp"
app:cardCornerRadius="20dp"
app:cardElevation="10dp"
app:cardMaxElevation="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="cardMaxElevation最大仰角"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginTop="10dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true"
app:contentPadding="2dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/cheese_1"/>
</android.support.v7.widget.CardView>
<TextView
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="cardUseCompatPadding 设置内边距\n
cardPreventCornerOverlap 是否添加内边距(避免内容与边缘重叠) \n
contentPadding 边距\
android:clickable配合foreground使用\n
cardElevation 海拔(z轴) 阴影\n
android:foreground=?attr/selectableItemBackground这个属性会在 Lollipop 上自动加上 Ripple 效果"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
CardView的基本属性
<attr name="cardBackgroundColor" format="color" />
<attr name="cardCornerRadius" format="dimension" />
<attr name="cardElevation" format="dimension" />
<attr name="cardMaxElevation" format="dimension" />
<attr name="cardUseCompatPadding" format="boolean" />
<attr name="cardPreventCornerOverlap" format="boolean" />
<attr name="contentPadding" format="dimension" />
<attr name="contentPaddingLeft" format="dimension" />
<attr name="contentPaddingRight" format="dimension" />
<attr name="contentPaddingTop" format="dimension" />
<attr name="contentPaddingBottom" format="dimension" />
attr name="cardBackgroundColor" format="color" 背景颜色
attr name="cardCornerRadius" format="color" 圆角半径
attr name="cardElevation" format="dimension" 海拔(z轴) 阴影
attr name="cardMaxElevation" format="dimension" 最大仰角
attr name="cardUseCompatPadding" format="boolean" 设置内边距,V21+的版本和之前的版本仍旧具有一样的计算方式
attr name="cardPreventCornerOverlap" format="boolean" 是否添加内边距(避免内容与边缘重叠)
attr name="contentPadding" format="dimension" 边距
为你的 Card 添加点击效果
当使用 CardView 的场合是作为列表中的一个 Item 且直接单击 Item 有相应的操作,那么就有必要加上视觉反馈来告诉用户这个 Card 是可点击的。
如果你是用了 AppCompat v7 支持库:
那么你可以直接给 CardView 加上android:foreground="?attr/selectableItemBackground" 这个属性会在 Lollipop 上自动加上 Ripple 效果,在旧版本则是一个变深/变亮的效果。
效果果:
转载请注明原文地址: https://ju.6miu.com/read-668003.html