这需要使用官方的 MaterialCardView
(扩展自androidx.cardview.widget.CardView
),以及至少版本1.1.0的Material组件库。
在布局中添加MaterialCardView
:
<com.google.android.material.card.MaterialCardView
style="@style/CustomCardViewStyle"
...>
</com.google.android.material.card.MaterialCardView>
定义自定义样式继承材料卡片样式(例如Widget.MaterialComponents.CardView
),并使用shapeAppearanceOverlay
属性:
<style name="CustomCardViewStyle" parent="@style/Widget.MaterialComponents.CardView">
<item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay_card_custom_corners</item>
</style>
<style name="ShapeAppearanceOverlay_card_custom_corners" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">4dp</item>
<item name="cornerSizeTopLeft">8dp</item>
<item name="cornerSizeBottomRight">16dp</item>
<item name="cornerSizeBottomLeft">0dp</item>
</style>
![enter image description here](https://istack.dev59.com/Z9RlY.webp)
您也可以通过编程实现。
只需要将自定义的ShapeAppearanceModel
应用于卡片的角落。
例如:
float radius = getResources().getDimension(R.dimen.my_corner_radius);
cardView.setShapeAppearanceModel(
cardView.getShapeAppearanceModel()
.toBuilder()
.setTopLeftCorner(CornerFamily.ROUNDED,..)
.setTopRightCorner(CornerFamily.ROUNDED,..)
.setBottomRightCorner(CornerFamily.ROUNDED,radius)
.setBottomLeftCornerSize(0)
.build());
注意: 它需要使用库的1.1.0版本。
使用Jetpack Compose,您可以在 Card
中使用 shape
参数。
例如:
Card(
shape = RoundedCornerShape(
topStart = 4.dp,
topEnd = 8.dp,
bottomEnd = 16.dp,
bottomStart = 2.dp,
)
){
Text("Content Card")
}
![enter image description here](https://istack.dev59.com/3rIu2.webp)