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

1,221 阅读1分钟

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

效果.png

让我们来学习吧

实现

1.引用

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

2.xml中使用


<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"//点击波纹效果的颜色

*注意 *

在使用使用MaterialCardView 的时候Activity的Theme必须是Theme.MaterialComponents下的Theme否则回报错


Caused by: java.lang.IllegalArgumentException: The style on this

component requires your app theme to be Theme.MaterialComponents 

(or a descendant)

解决方案 :

继承Theme.MaterialComponents.Light.NoActionBar就可以了

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

总结

简单的使用了一下MaterialCardView,学习一下

资源

MaterialCardView用法源码