CardView是Android5.0MD风格中的其中一个控件,该控件自带阴影效果可以实现3D卡片的效果
首先我们需要添加MD风格依赖包
compile ‘com.android.support:design:26.+’ //导入md风格依赖包
然后导入cardview依赖包
compile ‘com.android.support:cardview-v7:26.0.0-alpha1’
实现一个普通的CardView
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_below="@+id/toolbar"
android:id="@+id/cardview"
android:layout_margin="10dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/logo"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是一个CardView"
android:textColor="@android:color/black"
android:textSize="20dp"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
效果图
但是这样还不够美观,我们可以为CardView添加圆角,只需要在cardview中添加一条属性
app:cardCornerRadius="8dp"
就能实现圆角效果,如图
同时还可以是改变阴影效果的深度,添加一条属性
app:cardElevation="10dp
效果如图
怎么样是不是很好看(- -)
CardView还有很多的属性提供给我们
XML 属性 | 用法 |
---|---|
app:cardCornerRadius | 设置CardView的圆角半径 |
app:cardElevation | 设置z轴高度,来控制阴影的大小 |
android:foreground | 设置CardView的前景 |
app:contentPadding | 设置内边距 |
app:cardBackgroundColor | 设置卡片背景颜色 |
app:cardMaxElevation | 设置最大z轴高度 |
CardView然后不止这些属性,还有很多属性没列出来,这些都是常用的属性,有空可以了解一下!!!
作者:郑权
原文链接:点击这里