Android在 5.0中添加了阴影效果主要通过CardView来实现,在低版本中通过android.support.v7.widget.CardView来实现。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/包名"
android:id="@+id/rlListItemGame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="2dp" >
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="3dp"
app:cardElevation="2dp"
app:cardUseCompatPadding="true" >
可以看到上面的代码中多了三个app:属性,
app:cardCornerRadius设置圆角大小,
app:cardElevation设置阴影大小,
app:cardBackgroundColor="#fff000" 设置背景色
android:foreground=”?android:attr/selectableItemBackground”可以使CardView点击产生波纹的效果,有触摸点向外扩散
app:cardUseCompatPadding,设置分割效果,它在5.0以下的系统中默认是true,但在5.0系统中默认为false,如果不设置 app:cardUseCompatPadding=”true”的话会造成在5.0系统的Android手机上不能看到阴影效果。
最后一定要记得加上
xmlns:app="http://schemas.android.com/apk/res-auto"
CardView的兼容lib(eclipse可用)http://download.youkuaiyun.com/detail/zhong1113/8707351
2. 案例使用
cardview相当于个升级版的FrameLayout
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:cardCornerRadius="3dp"
- app:cardBackgroundColor="#fff000"
- app:cardUseCompatPadding="true"
- app:cardElevation="2dp">
- <RelativeLayout
- android:padding="10dp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <LinearLayout
- android:id="@+id/ll"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center_vertical"
- android:orientation="horizontal" >
- <ImageView
- android:id="@+id/icon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@mipmap/ic_launcher" />
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="哈哈哈哈啊" />
- </LinearLayout>
- </RelativeLayout>
- </android.support.v7.widget.CardView>
参考
http://blog.youkuaiyun.com/jdsjlzx/article/details/49511215