卡片布局可以单独使用,也可以放到ListView和RecyclerView中当做列表项里的内容来使用。本文都是放到ListView中使用。
一、使用ShapeDrawable资源实现卡片布局
实现效果图:
(1)list_item.xml这个文件是用来设置ListView中每一项的卡片内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/card_style"
android:padding="10dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/cardTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="18sp"
android:textColor="#000000"
android:text="我的数据" />
<ImageView
android:id="@+id/cardImg"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:src="@drawable/barchar"
/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#000000"/>
<TextView
android:id="@+id/cardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#000000"
android:text="这里放一些内容,这里放一些图片,这里扩展性很强"/>
</LinearLayout>
(2)card_style.xml 这个文件是实现卡片效果的关键,用的是ShapeDrawable资源,这个文件放在drawable目录下。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--定义矩形四个角的弧度-->
<corners android:radius="10dp" />
<!--填充颜色-->
<solid android:color="#ffffff" />
<!--为矩形绘制边框-->
<stroke android:color="#000000"
android:width="1dp"/>