android基础组件--CardView

来源:互联网 发布:福建随行软件下载 编辑:程序博客网 时间:2024/06/08 01:35


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.csdn.net/detail/zhong1113/8707351


2. 案例使用

cardview相当于个升级版的FrameLayout

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="wrap_content"  
  6.     app:cardCornerRadius="3dp"  
  7.     app:cardBackgroundColor="#fff000"  
  8.     app:cardUseCompatPadding="true"  
  9.     app:cardElevation="2dp">  
  10.   
  11.     <RelativeLayout  
  12.         android:padding="10dp"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content">  
  15.   
  16.         <LinearLayout  
  17.             android:id="@+id/ll"  
  18.             android:layout_width="match_parent"  
  19.             android:layout_height="wrap_content"  
  20.             android:gravity="center_vertical"  
  21.             android:orientation="horizontal"   >  
  22.   
  23.             <ImageView  
  24.                 android:id="@+id/icon"  
  25.                 android:layout_width="wrap_content"  
  26.                 android:layout_height="wrap_content"  
  27.                 android:src="@mipmap/ic_launcher" />  
  28.   
  29.             <TextView  
  30.                 android:id="@+id/text"  
  31.                 android:layout_width="wrap_content"  
  32.                 android:layout_height="wrap_content"  
  33.                 android:text="哈哈哈哈啊" />  
  34.   
  35.         </LinearLayout>  
  36.   
  37.     </RelativeLayout>  
  38.   
  39. </android.support.v7.widget.CardView> 

参考

http://blog.csdn.net/jdsjlzx/article/details/49511215





0 0
原创粉丝点击