CardView 卡片布局的使用

CardView继承自FrameLayout类,可以在一个卡片布局中一致性的显示内容。
卡片可以包含圆角和阴影.CardView是一个Layout,可以布局其他View。
CardView被包装为一种布局,并且经常在ListView和RecyclerView的Item布局中,作为一种容器使用。
以后会继续完善,谢谢大家,好吧,也谢谢自己能找时间写下来……

老规矩先贴效果图:

这里写图片描述

使用步骤:

添加依赖

可以直接在Module中的build.gradle中添加

compile 'com.android.support:cardview-v7:24.0.0'

可以在Module Settings中add

这里写图片描述

布局fragment_fenlei.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="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"
    tools:context="com.zhuowang.wangzhe.myapplication2.fragment.FenLeiFragment">

    <android.support.v7.widget.CardView
        android:id="@+id/fenlei_card"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="5dp"
        card_view:cardBackgroundColor="@android:color/white" ---》
        card_view:cardCornerRadius="20dp" ---》
        card_view:cardElevation="24dp" ---》
        android:clickable="false" ---》
        android:foreground="?android:attr/selectableItemBackground"> ---》

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <ImageView
                android:id="@+id/card_iv"
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:scaleType="centerCrop"
                android:src="@mipmap/untitled" />
            <TextView
                android:layout_width="match_parent"
                android:text="nihao"
                android:background="@android:color/holo_green_dark"
                android:layout_height="40dp" />

            <TextView
                android:id="@+id/card_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!" />
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

1、card_view:cardBackgroundColor=”@android:color/white” 设置CardView的背景颜色
在这里有个疑问,设置背景颜色,用android:background不可以吗?!本人亲测,使用background属性是无法设置CardView的背景色的
2、card_view:cardCornerRadius=”20dp” 圆角的半径,即,CardView四个角的弧度
3、card_view:cardElevation=”24dp” 投放的阴影,有点立体的感觉,好吧,其实我想到的是iPhone
4、默认情况,CardView是不可点击的,并且没有任何的触摸反馈效果。
可以使CardView点击产生波纹的效果,有触摸点向外扩散,这个效果需要添加下面两个属性:
android:clickable=”true” –>如果为false,没有效果
android:foreground=”?android:attr/selectableItemBackground”

java中的代码

 CardView cardView= (CardView) view.findViewById(R.id.fenlei_card);
        ImageView iv= (ImageView) view.findViewById(R.id.card_iv);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(),"this is iv",Toast.LENGTH_SHORT).show();
            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值