效果
以前实现这种效果需要自己做个背景图,阴影效果什么的不是很好
Android 5.0给了cardview来显示丰富多彩的布局,并且提供了兼容包,官方文档:
CardView
extends the FrameLayout
class and lets you show information inside cards that have a consistent look
on any app. CardView
widgets can have shadows and rounded corners.
To create a card with a shadow, use the android:elevation
attribute. CardView
uses real elevation and dynamic
shadows and falls back to a programmatic shadow implementation on earlier versions. For more information, see Compatibility.
Here's how to specify properties of CardView
:
- To set the corner radius in your layouts, use the
card_view:cardCornerRadius
attribute. - To set the corner radius in your code, use the
CardView.setRadius
method. - To set the background color of a card, use the
card_view:cardBackgroundColor
attribute.
中文翻译版:
CardView继承自FrameLayout类,可以在一个卡片布局中一致性的显示内容,卡片可以包含圆角和阴影。
可以使用android:elevation属性,创建一个阴影的卡片。
怎样指定CardView的属性:
1、使用android:cardCornerRadius属性指定圆角半径
2、使用CardView.setRadius 设置圆角半径。
3、使用 android:cardBackgroundColor属性设置卡片颜色
代码中使用 CardView, 引用Google提供的cardview兼容库 cardview-v7
-----------------------------------------------------
布局中:
<android.support.v7.widget.CardView xmlns:android="https://schemas.android.com/apk/res/android" xmlns:card_view="https://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" card_view:cardCornerRadius="3dp" > <RelativeLayout android:layout_width="match_parent" android:layout_height="160dp" android:padding="8dp" > <ImageView android:id="@+id/pic" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:scaleType="centerCrop" /> <TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="8dp" android:clickable="true" android:gravity="right|bottom" android:textColor="@android:color/white" android:textSize="20sp" /> </RelativeLayout> </android.support.v7.widget.CardView>
代码中:
//对CardView进行设置 CardView cardView = (CardView) itemView; //cardView.setCardBackgroundColor(Color.RED); cardView.setCardElevation(10);