RecyclerView与CardView的使用(二)

简介: RecyclerView与CardView的使用(二)

3.当RecyclerView遇上CardView


CardView继承自Framelayout,所以FrameLayout所有属性CardView均可以直接拿来用,不过CardView还有自己独有的属性。下面我们将item布局的父标签换成CardView。并且用RelativeLayout包裹两个子控件。代码如下:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/lyj_image"
            android:layout_width="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginRight="5dp"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/lyj_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/lyj_image"
            android:layout_marginTop="40dp" />
    </RelativeLayout>
</android.support.v7.widget.CardView>


运行效果图如下:


37.png


其每个item变成了一个卡片的样式,如果色彩搭配的好,那么界面将如上线的APP一样酷炫。


下面来介绍CardView独有的属性:


app:cardElevation 阴影的大小

app:cardMaxElevation 阴影最大高度

app:cardBackgroundColor 卡片的背景色

app:cardCornerRadius 卡片的圆角大小

app:contentPadding 卡片内容于边距的间隔◦card_view:contentPaddingBottom

app:contentPaddingTop

app:contentPaddingLeft

app:contentPaddingRight

app:contentPaddingStart

app:contentPaddingEnd

app:cardUseCompatPadding 设置内边距,V21+的版本和之前的版本仍旧具有一样的计算方式

app:cardPreventConrerOverlap 在V20和之前的版本中添加内边距,这个属性为了防止内容和边角的重叠


下面简单设置几个属性:


app:cardBackgroundColor="#EEC900":卡片背景为黄色。


app:cardCornerRadius="10dp":卡片圆角半径为10dp。


app:cardPreventCornerOverlap="true":防止内容与边角重叠


app:cardUseCompatPadding="true":设置边距


app:contentPadding="10dp":边距的间隔大小为10dp


运行一下效果图如下所示:

38.png


相关文章
|
11月前
|
API Android开发
CardView的那点事儿
CardView的那点事儿
86 0
|
开发工具 Android开发
RecyclerView与CardView的使用(一)
RecyclerView与CardView的使用(一)
126 0
RecyclerView与CardView的使用(一)
SwipeRefreshLayout与RecyclerView的巧夺天工(二)
SwipeRefreshLayout与RecyclerView的巧夺天工(二)
62 0
SwipeRefreshLayout与RecyclerView的巧夺天工(二)
SwipeRefreshLayout与RecyclerView的巧夺天工(一)
SwipeRefreshLayout与RecyclerView的巧夺天工(一)
66 0
|
Android开发
2-VVI-材料设计之CardView
零、前言 [1].CardView extends FrameLayout [2].一个带圆角和阴影的FrameLayout,FrameLayout怎么用,它就怎么用 [3].
1084 0