Android Material 新控件之更强大的CardView支持自定义边框 MaterialCardView(二)

 

前言

MaterialCardView提供了CardView的所有功能,但增加了一些自定义属性,使用起来更加方便实用。在Cardview 的基础上增加了 strokeColor strokeWidth 自定义边框等属性

一、MaterialCardView是什么?

MaterialCardView是提供了更多自定义属性的CardView

二、使用步骤

1.引入库

代码如下(示例):

  implementation "com.google.android.material:material:1.1.0"

注意:在使用使用MaterialCardView 的时候Activity的Theme必须是Theme.MaterialComponents下的Theme

android:theme="@style/Theme.MaterialComponents.Light.NoActionBar"

     <activity
            android:name=".module.main.ui.activity.MainActivity"
            android:theme="@style/Theme.MaterialComponents.Light.NoActionBar"
            android:configChanges="keyboardHidden|screenSize|orientation"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden|adjustResize"
            tools:ignore="LockedOrientationActivity" >
     </activity>

异常:

Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant)

2.使用

 <com.google.android.material.card.MaterialCardView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="16dp"
                        android:layout_marginTop="14dp"
                        android:layout_marginRight="16dp"
                        android:layout_marginBottom="20dp"
                        app:contentPadding="20dp"
                        app:cardElevation="8dp"
                        app:cardCornerRadius="8dp"
                        app:rippleColor="@color/color_23d41e"
                        app:strokeColor="@color/color_23d41e"
                        app:strokeWidth="5dp">

                        <LinearLayout
                            android:background="@color/color_white"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical"
                            android:paddingBottom="48dp">

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginBottom="4dp"
                                android:text="标题" />

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="测试" />
                        </LinearLayout>
 </com.google.android.material.card.MaterialCardView>

 


3:属性介绍

    app:strokeColor="@color/color_23d41e" //边框的颜色
    app:strokeWidth="5dp" //边框的宽度
    app:cardElevation="8dp" //阴影大小
    app:cardCornerRadius="8dp"//角度大小
    app:rippleColor="@color/color_23d41e"//点击波纹效果的颜色


总结

1:Demo源码

2:MaterialCardView源码

MaterialCardView的简单使用方法特此记录,有用的欢迎拿走,来都来了点个赞吧

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值