Android CardView角半径

Android CardView角半径,android,android-appcompat,android-cardview,Android,Android Appcompat,Android Cardview,有没有办法使CardView在顶部仅具有圆角半径 <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layo

有没有办法使CardView在顶部仅具有圆角半径

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="10dp"
    >

除非尝试扩展Android
CardView
类,否则无法从
XML
自定义该属性

尽管如此,还是有办法取得这种效果的

将一个
cardwiew
放置在另一个
cardwiew
内部,并将透明背景应用于外部
cardwiew
并移除其拐角半径(
“cornerRadios=0dp”
)。例如,内部
CardView
的拐角半径值为3dp。然后在内部
cardwiew
上应用marginTop,这样它的底部边界将被外部
cardwiew
剪切。这样,内部
CardView
的底角半径将被隐藏

XML代码如下所示:

 <android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view_outer"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_gravity="center"
    card_view:cardBackgroundColor="@android:color/transparent"
    card_view:cardCornerRadius="0dp"
    card_view:cardElevation="3dp" >

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view_inner"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:layout_marginTop="3dp"
        card_view:cardBackgroundColor="@color/green"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="0dp" >
    </android.support.v7.widget.CardView>
</android.support.v7.widget.CardView>

视觉效果如下:

 <android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view_outer"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_gravity="center"
    card_view:cardBackgroundColor="@android:color/transparent"
    card_view:cardCornerRadius="0dp"
    card_view:cardElevation="3dp" >

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view_inner"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:layout_marginTop="3dp"
        card_view:cardBackgroundColor="@color/green"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="0dp" >
    </android.support.v7.widget.CardView>
</android.support.v7.widget.CardView>


始终将您的内容放在您的内部
cardwiew
中。外部CardView仅用于“隐藏”内部的底部圆角
CardView

您可以使用此可绘制xml并将其设置为CardView的背景:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffffff"/>

    <stroke android:width="1dp"
        android:color="#ff000000"
        />

    <padding android:left="1dp"
        android:top="1dp"
        android:right="1dp"
        android:bottom="1dp"
        />

    <corners 
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp"/>
</shape>

您需要做两件事:

1) 在您的CardView上调用
setPreventCornerOverlap(false)

2) 将四舍五入的图像视图放入CardView内部

关于将imageview四舍五入,我遇到了同样的问题,因此我制作了一个库,您可以在每个角设置不同的半径。最后我得到了下面我想要的结果


我编写了一个可绘制的库来定制圆角位置,它看起来如下:

您可以在此处获取此库:



当卡位于屏幕最底部时,有一个如何实现的示例。如果有人有这样的问题,就这样做:

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="-5dp"
    card_view:cardCornerRadius="4dp">

    <SomeView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp">

    </SomeView>

</android.support.v7.widget.CardView>

卡片视图的底边距为负数。卡片视图中的视图具有相同但正的底部边距。通过这种方式,圆形部分隐藏在屏幕下方,但所有内容看起来都完全相同,因为内部视图有一个计数器边距。

您可以使用库:


在Android Studio中实现此功能的最简单方法如下所述:

第一步:
build.gradle
中的依赖项行下方写入:

compile 'com.android.support:cardview-v7:+'
第二步:
在xml文件中复制以下代码,以集成
CardView

要使
cardCornerRadius
正常工作,请确保在父布局中包含以下行:
xmlns:card\u视图=”http://schemas.android.com/apk/res-auto“

记住使用
card\u view
作为使用
cardCornerRadius
属性的命名空间

例如:
card\u视图:cardCornerRadius=“4dp”

XML代码:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view_outer"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_gravity="center"
    card_view:cardBackgroundColor="@android:color/transparent"
    card_view:cardCornerRadius="0dp"
    card_view:cardElevation="3dp" >

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view_inner"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:layout_marginTop="3dp"
        card_view:cardBackgroundColor="@color/green"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="0dp" >
    </android.support.v7.widget.CardView>

</android.support.v7.widget.CardView>

如果以编程方式设置卡片背景,请使用
cardView.setCardBackgroundColor()
而不是
cardView.setBackgroundColor()
,并确保在cardView.xml上使用
app:cardPreventCornerOverlap=“true”
。那帮我修好了


顺便说一句,上面的代码(在引号中)是用Kotlin而不是Java编写的。如果您正在使用java,请使用java等效工具。

实现这一点的简单方法是:

1.制作一个带有圆角的自定义背景资源(如矩形)

2.使用命令设置此自定义背景-

cardView = view.findViewById(R.id.card_view2);
cardView.setBackgroundResource(R.drawable.card_view_bg);
这对我有用

我用左上角和右下角的半径制作的
XML
布局

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />
<corners android:topLeftRadius="18dp" android:bottomRightRadius="18dp" />
</shape>

在您的情况下,只需要更改topLeftRadius和topRightRadius

如果您的布局与卡片视图的角重叠,并且可能具有不同的颜色,那么您可能需要为布局使用不同的背景资源文件,并在xml中将此背景资源设置为布局

我尝试并测试了上述方法。希望这对您有所帮助。

您可以使用官方提供的标准

在布局中使用:

<com.google.android.material.card.MaterialCardView
        style="@style/MyCardView"
        ...>
您还可以使用:

<com.google.android.material.card.MaterialCardView
     app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MaterialCardView.Cut"
     ...>

结果是:


您可以将ImageView放入CardView中,并将这些属性添加到ImageView中:

android:cropToPadding="true"
android:paddingBottom="15dp"

这样你就可以去掉底部的圆角,但请记住,这是以剪掉一小部分图像为代价的。

注意:如果你只想在底部实现圆角,而在顶部实现规则角,这里有一个解决方法。如果您希望cardview的所有四个角具有不同的半径,则此操作将不起作用。您必须使用material cardview或使用某些第三方库

以下是对我有效的方法:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#F9F9F9">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/profile_bg"/>

    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/cvProfileHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="32dp">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:orientation="vertical"
            android:background="@drawable/profile_bg"
            android:id="@+id/llProfileHeader"
            android:gravity="center_horizontal">

            <!--Enter your code here-->

        </LinearLayout>
    
    </androidx.cardview.widget.CardView>

</RelativeLayout>

总共有两个cardview。第二个cardview是一个具有圆角的视图(通常在所有边上),并将在其下保存所有其他子视图。上面的第一个cardview也位于同一标高(标高),具有相同的背景,但只有第二个cardview高度的一半左右,没有圆角(只有通常的锐角)。通过这种方式,我能够在底部实现部分圆角,在顶部实现正常圆角。但对于所有四个面,您可能必须使用材质cardview


您可以执行与此相反的操作,以获得顶部的圆角和底部的常规圆角,即让第一个cardview具有圆角,第二个cardview具有常规拐角。

注意阴影不正确(底角阴影仍为圆角),前景选择器可能更明显地不正确。我认为@shreedhar bhat描述的简单方法更好。你知道cardCornerRadius的默认值是多少吗?是4dp吗?我不知道为什么每个人都把这个标记为答案。这不是正确的答案。我试过了,但没用
android:cropToPadding="true"
android:paddingBottom="15dp"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#F9F9F9">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/profile_bg"/>

    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/cvProfileHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="32dp">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:orientation="vertical"
            android:background="@drawable/profile_bg"
            android:id="@+id/llProfileHeader"
            android:gravity="center_horizontal">

            <!--Enter your code here-->

        </LinearLayout>
    
    </androidx.cardview.widget.CardView>

</RelativeLayout>