CardVie

来源:互联网 发布:炫酷黑客手机源码 编辑:程序博客网 时间:2024/06/12 23:53

cardView 介绍: 
文档地址: 
http://developer.android.com/reference/android/support/v7/widget/CardView.html

CardView继承自FrameLayout,这个控件能够让你能用卡牌的方式来展示信息,

能够使所有的APP有统一的风格。这个控件能设置阴影和圆角。

  CardView特别的属性如下:

  • android:cardCornerRadius:在布局中设置card圆角的大小
  • CardView.setRadius:在代码中设置card圆角的大小
  • android:cardBackgroundColor:在布局中设置card背景的颜色
  • android:elevation:设置阴影的大小
  • card_view:cardElevation 阴影的大小
  • card_view:cardMaxElevation 阴影最大高度
  • card_view:cardCornerRadius 卡片的圆角大小
  • card_view:contentPadding 卡片内容于边距的间隔 
    card_view:contentPaddingBottom 
    card_view:contentPaddingTop 
    card_view:contentPaddingLeft 
    card_view:contentPaddingRight 
    card_view:contentPaddingStart 
    card_view:contentPaddingEnd
  • card_view:cardUseCompatPadding 设置内边距,V21+的版本和之前的版本仍旧具有一样的计算方式
  • **card_view:cardPreventConrerOverlap 
    在V20和之前的版本中添加内边距,这个属性为了防止内容和边角的重叠**

使用方法:

首先导入依赖包:

dependencies {    compile fileTree(include: ['*.jar'], dir: 'libs')    compile 'com.android.support:cardview-v7:23.1.1'}
  • 1
  • 2
  • 3
  • 4
  • 5

然后在布局声明:

    <android.support.v7.widget.CardView        xmlns:card_view="http://schemas.android.com/apk/res-auto"        android:id="@+id/card_view"        android:layout_gravity="center"        android:layout_marginTop="10dp"        android:layout_marginLeft="10dp"        android:layout_marginRight="10dp"        android:layout_width="match_parent"        android:layout_height="200dp"        android:clickable="true"        card_view:contentPadding="10dp"        android:foreground="?android:attr/selectableItemBackground">        card_view:cardBackgroundColor="@color/alpha_actmode"        card_view:cardElevation="1dp"        card_view:cardCornerRadius="4dp">        <TextView            android:id="@+id/info_text"            android:layout_gravity="center_horizontal"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="cardView"            />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="bottom|center_horizontal"            android:text="确定"            />    </android.support.v7.widget.CardView>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

cardView 大部分适用于 listView 或者recyclerView的item中。