使用CardView实现圆角或圆形的效果

前言

CardView是Android 5.0 中一种全新的控件,可以实现圆角和阴影效果。

添加依赖

compile ‘com.android.support:cardview-v7:23.4.0’

开始使用

CardView是一个新增的UI控件。我们通过源码可以看出:
public class CardView extends FrameLayout{…}
它继承了FrameLayout布局,所以我们可以把它当成一个容器来使用。

常用属性
CardView_cardBackgroundColor:设置背景色
CardView_cardCornerRadius:设置圆角角度大小
CardView_cardElevation:设置z轴阴影大小
CardView_cardMaxElevation:设置z轴最大高度值
CardView_cardUseCompatPadding:是否使用CompadPadding,设置内边距,v21+的版本和之前的版本仍旧具有一样的计算方式
CardView_cardPreventCornerOverlap:是否使用PreventCornerOverlap,在v20和之前的版本中添加内边距,这个属性是为了防止卡片内容和边角的重叠
CardView_contentPadding:内容的padding
CardView_contentPaddingLeft:内容的左padding
CardView_contentPaddingTop:内容的上padding
CardView_contentPaddingRight:内容的右padding
CardView_contentPaddingBottom:内容的底padding

布局文件

    <android.support.v7.widget.CardView
                android:layout_width="@dimen/margin_84"
                android:layout_height="@dimen/margin_68"
                app:cardCornerRadius="@dimen/margin_5"
                app:cardElevation="0dp"
                app:cardUseCompatPadding="false">

                <ImageView
                    android:id="@+id/voucher_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:scaleType="fitXY"
                    android:src="@drawable/loadingimage"/>

            </android.support.v7.widget.CardView>

简单实现圆形ImageView

设置CardView宽高相等,CardView设置圆角的半径为宽高的一半,就是一个圆形效果了。例如:

<android.support.v7.widget.CardView
    android:id="@+id/cv_img_activity"
    android:layout_width="200dp"
    android:layout_height="200dp"
    app:cardCornerRadius="100dp"
    app:cardElevation="10dp"
    app:cardPreventCornerOverlap="true">

    <ImageView
        android:id="@+id/iv_cv_img_activity"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>

</android.support.v7.widget.CardView>
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中,可以使用 CardView 控件实现四周阴影效果CardView 内部已经实现了阴影效果,只需要将需要添加阴影的布局放在 CardView 中即可。 以下是使用 CardView 实现四周阴影效果的示例代码: 1. 在布局文件中添加 CardView 控件,并将需要添加阴影效果的布局放在 CardView 中。 ``` <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" app:cardCornerRadius="8dp" app:cardElevation="8dp" app:cardUseCompatPadding="true"> <!-- 需要添加阴影效果的布局 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是一个示例布局" android:textSize="24sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是一个示例布局的内容" android:textSize="16sp" /> </LinearLayout> </androidx.cardview.widget.CardView> ``` 在 CardView 控件中,使用 app:cardCornerRadius 属性可以设置圆角半径,使用 app:cardElevation 属性可以设置阴影大小,使用 app:cardUseCompatPadding 属性可以设置是否使用兼容性内边距。 2. 运行应用程序,即可看到添加了四周阴影效果的布局。 这样就可以使用 CardView 控件实现 Android 中的四周阴影效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值