Android卡片布局的两种实现方式

本文介绍了Android中卡片布局的两种实现方式,一种是通过ShapeDrawable资源,另一种是使用CardView控件。内容主要涉及如何将卡片布局应用到ListView中,并展示了在不同平台上的阴影效果实现。
摘要由CSDN通过智能技术生成

卡片布局可以单独使用,也可以放到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"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值