前言
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"//点击波纹效果的颜色
总结
MaterialCardView的简单使用方法特此记录,有用的欢迎拿走,来都来了点个赞吧
版权声明:本文为wu296606241原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。