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

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

1:使用:

使用MaterialCardView需要引入Material库:

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

注意!!!

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

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

错误

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>

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>

属性介绍:

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

用得上的人儿 给个赞呗,多谢围观.

MaterialCardView用法源码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容