效果图
关键代码
<!-- 添加android:clickable属性后,点击cardview会有水波纹效果 -->
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
app:cardBackgroundColor="@color/purple_500"
app:cardCornerRadius="10dp"
app:cardElevation="10dp"
app:contentPadding="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="CardView测试样例!"
android:textColor="@android:color/background_light" />
</androidx.cardview.widget.CardView>
CardView
是在Android5.0
上新增新控件,是在FrameLayout
的基础上添加了圆角和阴影效果,实现了立体感的控件。CardView
可以作为容器,一般应用在ListView
和RecyclerView
的Item
布局中,Item
布局可以再新增其他控件。
CardView
常用属性如下:
属性 | 含义 |
---|---|
app:cardBackgroundColor | 背景色 |
app:cardCornerRadius | 边缘弧度数 |
app:cardElevation | 高度 |
app:cardMaxElevation | 最大高度 |
app:cardUseCompatPadding | 设置内边距 |
app:contentPadding | 卡片边界距离内部的距离 |