简介
- Android 5.0 版本中新增。
- 继承自 FrameLayout 类。
- 可以设置圆角和阴影,使得控件具有立体性。
- 可以包含其他的布局容器和控件。
导包
implementation 'androidx.cardview:cardview:1.0.0'
属性
xml 属性
-
app:cardBackgroundColor:设置背景色
-
app:cardCornerRadius=“30dp”:设置圆角大小
-
app:cardElevation=“30dp”:设置 Z 轴阴影大小
-
app:cardMaxElevation=“30dp”:Z 轴最大高度值大小
-
app:cardPreventCornerOverlap=“false”:是否使用 PreventCornerOverlap
-
app:cardUseCompatPadding=“false”:配合 app:cardElevation=“30dp”,阴影效果
当 app:cardUseCompatPadding=“false” 时,阴影在外面
当 app:cardUseCompatPadding=“true” 时,阴影在里面
-
app:contentPadding=“30dp”:内容的 padding
-
app:contentPaddingBottom=“30dp”:内容的底 padding
-
app:contentPaddingLeft=“30dp”:内容的左 padding
-
app:contentPaddingRight=“30dp”:内容的右 padding
-
app:contentPaddingTop=“30dp”:内容的上 padding
代码属性
//对应 app:cardBackgroundColor
public void setCardBackgroundColor(@ColorInt int color)
public void setCardBackgroundColor(@Nullable ColorStateList color)
//对应 app:cardCornerRadius
public void setRadius(float radius)
//对应 app:cardElevation
public void setElevation(float elevation)
//对应 app:cardMaxElevation
public void setMaxCardElevation(float maxElevation)
//对应 app:contentPadding
public void setContentPadding(@Px int left, @Px int top, @Px int right, @Px int bottom)