CardView的使用

先上效果图:

这里写图片描述

就像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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值