效果图
一、CardView是什么?
CardView是一个视图容器,继承自FrameLayout,CardView像一张卡片,有阴影和圆角,这些属性也可以添加到其他视图组中。
二、使用步骤
1.布局
代码如下(示例):
<androidx.cardview.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="10dp"
android:padding="10dp"
app:cardBackgroundColor="@color/white"
app:cardElevation="10dp"
app:cardCornerRadius="5dp"
app:contentPadding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/img"
android:layout_width="120dp"
android:layout_height="match_parent"
android:background="@mipmap/fruit"/>
<TextView
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="水果水果水果水果水果水果水果水果"
android:textSize="14sp"
android:textColor="#333"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
2.属性
app:cardElevation:阴影的大小
app:cardCornerRadius:圆角的大小
app:contentPadding:卡片里内容距卡片边距的距离
app:cardBackgroundColor:卡片背景颜色
总结
CardView卡片式布局 可以使用于 商品列表 、明信片等