先上效果图:
就像CardView的名字一样,就是卡片的效果。
代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:layout_marginBottom="5dp"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<ImageView
android:id="@+id/pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:scaleType="fitCenter"
android:src="@drawable/img_artilce"/>
<TextView
android:id="@+id/text_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pic"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:text="I'm CardView"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
稍微解释一下:
1、首先使用CardView要先导入android-support-v7-cardview.jar
AndroidStudio直接在build.gradle里添加
compile ‘com.android.support:cardview-v7:22.2.0’
(Eclipse用户请自行查找)
1、CardView继承自FrameLayout,所以完全可以作为根布局容器来使用(我上边的代码是RelativeLayout 作为的跟布局 是因为默认创建出来 懒得删了……)
2、定制CardView的圆角和周边阴影大小:
添加 xmlns:app=”http://schemas.android.com/apk/res-auto”
圆角大小: app:cardCornerRadius=”8dp”
阴影大小: app:cardElevation=”8dp”
3、CardView默认是不支持点击的也没有触摸反馈。要想点击并有反馈
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
我这里的是触摸有水波纹效果,想要其他效果的朋友可以自行查找。
好啦,都是基础的使用。其他的用法,大家用一下就知道啦。有问题欢迎提出。
再次推荐一下宝哥的扣扣群,群内氛围和谐,有问题可以随便提。
群号:454430053