CardView的使用(可以实现圆角控件)

时间:2019-08-29
本文章向大家介绍CardView的使用(可以实现圆角控件),主要包括CardView的使用(可以实现圆角控件)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

简介:ardView继承自FrameLayout,它是一个带圆角背景和阴影的FrameLayout

一些什么实现方式就不赘述了,直接上用法

CardView的常用属性

  1、设置背景颜色 

app:cardBackgroundColor=" "

2、设置padding
app:contentPadding=" "
app:contentPaddingTop=" "
app:contentPaddingBottom=" "
app:contentPaddingLeft=" "
app:contentPaddingRight=" "

注意啊:上面是CardView设置背景颜色和padding的方式,如果你直接通过android:padding=" " 和android:background=" "设置,是无效的

3、设置Z轴的最大高度
app:cardMaxElevation=" "

4、点击之后的涟漪效果
android:clickable="true"
android:foreground="android:attr/selectableItemBackground"

注意:如果你给CardView设置了点击事件,就不需要设置android:clickable="true"了

5.设置CardView的圆角半径

app:cardCornerRadius =""

6设置CardView的前景

android:foreground =""

7设置内边距

app:contentPadding ="

例如在项目中的用法
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    ... >
    <!-- A CardView that contains a TextView -->
    <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_width="200dp"
        android:layout_height="200dp"
        card_view:cardCornerRadius="4dp">

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

总结:注意这个控件在5.0之下会有包裹不均匀的现象,但是在5.0之上会很好用

可以控制这个属性来适配app:cardPreventCormorOverlap

true:防止子View被CardView的圆角进行剪切 false: 允许剪切,主要用于适配Lollipop以下

by磊磊tua

原文地址:https://www.cnblogs.com/widgetbox/p/11428542.html