具有不同角半径的 CardView

新手上路,请多包涵

我有以下 CardView,我想为卡片中的每个角设置不同的半径。是否可以通过 XML 或编程方式更改它们?提前致谢。

 <android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    app:cardCornerRadius="0dp"
    app:cardElevation="0dp">
</android.support.v7.widget.CardView>

编辑 正如 Avinash 建议的那样,我正在寻找这个 lib github.com/captain-miao/OptionRoundCardview 的行为,但使用默认的 CardView 项目。如果无法单独更改它,这个库是一个很好的方法。

原文由 Juanje 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 664
2 个回答

它需要官方的 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>

在此处输入图像描述

您也可以通过编程方式实现它。

只需将自定义 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")
}

在此处输入图像描述

原文由 Gabriele Mariotti 发布,翻译遵循 CC BY-SA 4.0 许可协议

您可以创建自定义 xml 并将其命名为 rounded_corners.xml 如下所示:

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1dp"
    android:topLeftRadius="20dp"
    android:topRightRadius="30dp"
    android:bottomLeftRadius="40dp"
    android:bottomRightRadius="50dp"/>
<solid android:color="your_background_color" />
</shape>

然后将其用作 CardView 的背景:

 android:background="@drawable/rounded_corners"

编辑: 我刚刚注意到这可能适用于除 CardView 以外的所有其他视图,因此请参阅 此问题 以了解如何进行解决方法。

原文由 Vucko 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题