码迷,mamicode.com
首页 > 其他好文 > 详细

CardView卡片view

时间:2017-04-21 22:49:27      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:use   match   frame   style   extends   imp   mil   add   lis   

Android 5.0的另一个新增加的控件CardView

1.在Gradle中添加CardView包的依赖:

compile ‘com.android.support:cardview-v7:21.0.+‘

2.基本使用:

CardView是一个新增加的UI控件,我们先在代码中定义一个CardView的变量,然后查看源码看看这是个什么玩意。源码先:

public class CardView extends FrameLayout implements CardViewDelegate {  
            ...  
} 

从源码看,CardView继承FrameLayout,所以CardView是一个ViewGroup,我们可以在里面添加一些控件进行布局。既然CardView继承FrameLayout,而且Android中早已有了FrameLayout布局,为什么还有使用CardView这个布局控件呢?我们先来看看官网对此类的注释:

  A FrameLayout  with a rounded corner background and shadow. 

3. CardView新增属性:

  • CardView_cardBackgroundColor 设置背景色
  • CardView_cardCornerRadius 设置圆角大小
  • CardView_cardElevation 设置z轴阴影
  • CardView_cardMaxElevation 设置z轴最大高度值
  • CardView_cardUseCompatPadding 是否使用CompadPadding
  • CardView_cardPreventCornerOverlap 是否使用PreventCornerOverlap
  • CardView_contentPadding 内容的padding
  • CardView_contentPaddingLeft 内容的左padding
  • CardView_contentPaddingTop 内容的上padding
  • CardView_contentPaddingRight 内容的右padding
  • CardView_contentPaddingBottom 内容的底padding

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

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

4.使用:
1、普通使用效果

<android.support.v7.widget.CardView  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content">  
        <TextView  
            android:layout_width="match_parent"  
            android:layout_height="70dp"  
            android:text="正常使用效果"  
            android:gravity="center_horizontal|center_vertical"  
            android:textColor="#000000"  
            android:textSize="20sp"  
            android:padding="10dp"  
            android:layout_margin="10dp"/>  
</android.support.v7.widget.CardView>  

效果图: 
技术分享

2、增加背景色和圆角的效果,注意我们此时使用background属性是没效果的:

<android.support.v7.widget.CardView  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        app:cardBackgroundColor="#669900"  
        app:cardCornerRadius="10dp">  
        ...  
</android.support.v7.widget.CardView>  

效果图: 
技术分享

3、设置z轴阴影

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="#669900"
    app:cardElevation="20dp"
    app:cardCornerRadius="10dp">
    ...
</android.support.v7.widget.CardView>

效果图: 
技术分享

4.在ListView、RecyclerView中一定也有不错的效果,那么现在我们来使用下:首先定义RecyclerView的item的布局:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:app="http://schemas.android.com/apk/res-auto"  
        app:cardBackgroundColor="#80cbc4"  
        app:cardCornerRadius="10dp"  
        app:cardPreventCornerOverlap="true"  
        app:cardUseCompatPadding="true"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content">  
        <RelativeLayout  
            android:layout_width="match_parent"  
            android:layout_height="100dp"  
            android:padding="5dp">  
            <ImageView  
                android:id="@+id/picture"  
                android:layout_width="match_parent"  
                android:layout_height="match_parent"  
                android:layout_centerInParent="true"  
                android:scaleType="centerCrop" />  
            <TextView  
                android:clickable="true"  
                android:id="@+id/name"  
                android:layout_width="match_parent"  
                android:layout_height="match_parent"  
                android:layout_marginBottom="10dp"  
                android:layout_marginRight="10dp"  
                android:gravity="right|bottom"  
                android:textColor="@android:color/white"  
                android:textSize="24sp" />  
        </RelativeLayout>  
  
</android.support.v7.widget.CardView>  

  

 技术分享

CardView卡片view

标签:use   match   frame   style   extends   imp   mil   add   lis   

原文地址:http://www.cnblogs.com/ganchuanpu/p/6746229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!