CardView

安卓5.0引进了一个新的控件叫做CardView 本质上可以看作是一个带有圆角和阴影以及海拔的FrameLayout CardView 可以包裹一个布局,并且经常在布局中作为每一项的container。

使用

依赖:compile 'com.android.support:cardview-v7:23.2.1'
在布局中使用

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <!-- Main Content View -->
    <RelativeLayout>
        ...
    </RelativeLayout>
</android.support.v7.widget.CardView>

设置

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardBackgroundColor="#E6E6E6"
    card_view:cardCornerRadius="8dp"
    card_view:cardElevation="8dp">

    <!-- Main Content View -->
    <RelativeLayout>
        ...
    </RelativeLayout>
</android.support.v7.widget.CardView>

使用代码设置:

CardView card = ...
card.setCardBackgroundColor(Color.parseColor("#E6E6E6"));
card.setMaxCardElevation(0.0);
card.setRadius(5.0);
``
#增加波纹效果

android:clickable=”true”
android:foreground=”?android:attr/selectableItemBackground”>
“`

版本支持

在L之前的版本,CardView 通过增加间距来支持圆角半径,同样的对于阴影,L之前的版本,`CardView 通过添加内容之间的间距和阴影的绘制来获得阴影。
按照文档:两边的间距的值等于maxCardElevation + (1 - cos45) * cornerRadius
上下间距的值:maxCardElevation*1.5 + (1 - cos45) * cornerRadius.
如果要指定自定义间距的值
card_view:contentPadding
更换背景:
card_view:cardBackgroundColor`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值