您的位置:首页 > 移动开发 > Android开发

Android CardView 的使用

2016-05-27 16:51 831 查看

CardView

CardView继承至FrameLayout类,可以在一个卡片布局中一致性的显示内容,卡片可以包含圆角和阴影。CardView是一个Layout,可以布局其他View。

CardView的 常用属性

card_view:cardElevation 阴影的大小

card_view:cardMaxElevation 阴影最大高度

card_view:cardBackgroundColor 卡片的背景色

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和之前的版本中添加内边距,这个属性为了防止内容和边角的重叠

demo演示

这个demo 通过RecyclerView和CardView实现了瀑布流的效果,你可以看下前面写的一篇文章,有其余的代码.

Android RecyclerView 实现瀑布流效果

使用cardview将每个RecyclerView的组件包裹起来,即可实现卡片效果.

<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="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="#22eec900"
app:cardCornerRadius="10dp"
app:cardMaxElevation="1dp"
app:cardElevation="1dp">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<!-- adjustViewBounds 是否保持原图宽高比 -->
<!-- 中心缩放 -->
<ImageView
android:id="@+id/id_iv_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@string/image"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/id_tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
</android.support.v7.widget.CardView>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: