0

Android Studio版本:2.3.3,Android 4.3CardView:图像的边角在Android 4.3中不是圆角的?

我正在使用CardView并希望圆形图像的边角。

布局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="@dimen/card_width" 
    android:layout_height="@dimen/card_width" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:orientation="vertical"> 
    <android.support.v7.widget.CardView 
     android:id="@+id/card_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     card_view:cardCornerRadius="15dp"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="center"> 
      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:scaleType="centerCrop" 
       android:src="@drawable/img_lights" /> 
     </RelativeLayout> 
    </android.support.v7.widget.CardView> 
</LinearLayout> 

正如你可以SES我设置

card_view:cardCornerRadius="15dp

下面是结果:

Not round image

但图像的角不圆。为什么?

+1

可能重复[如何使圆角的ImageView?](https://stackoverflow.com/questions/2459916/how-to-make-an-imageview-with-rounded-corners) –

回答

-1

添加到app

app:cardCornerRadius="15dp" 
app:cardUseCompatPadding="true" 

然后导入必要的值!

+0

没有帮助: 'xmlns:app =“http://schemas.android.com/apk/res-auto”app:cardCornerRadius =“15dp' – user8542613

+0

我发现了这个问题,这是因为我从Android 4.3开始,如果我从Android 6.0开始 - 然后一切正常 – user8542613

+0

我发现你的东西添加在布局上,我已经编辑了答案,并考虑upvoting,如果它的工作原理! – Xenolion

1

正如cardUseCompatPadding属性的docs说:

CardView增加了额外的填充棒棒堂之前绘制的平台上阴影。

这可能会导致卡片在棒棒糖和棒棒糖之间有不同的尺寸。如果您需要将CardView与其他视图对齐,则可能需要api特定于版本的维度资源来说明更改。作为替代方案,您可以将此标志设置为true,并CardView将增加对平台棒棒糖和之后的相同填充值..

应用app:cardUseCompatPadding="true"CardView

+0

已更新的回答 – azizbekian

+0

我回答了同样在这里下面,但我很惊讶,他说这不起作用,他低估了它!他怎么不对他工作?@azizbekian – Xenolion

0

尝试添加以下drawable作为图像的背景。

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scaleType="centerCrop" 
    android:background="drawable/rounded" 
    android:src="@drawable/img_lights" /> 

绘制/ rounded.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 

    <solid android:color="#FFFFFF" /> 
    <corners android:radius="15dp" /> 

</shape> 
+0

我知道,通过“形状”我可以做到这一点。我只想通过CardView元素来做到这一点。 – user8542613

0

您所看到的填充是支持年龄大于20 API为了让您的布局看起来一样要么在预棒棒糖结果(和CardView小部件)或棒棒糖和较新的,Android引入了一些额外的属性。

因为重叠内容 - 四舍五入 - 对于较旧的Android版本来说很困难,所以cardPreventCornerOverlap已被添加并且已被默认设置为true。这就是为什么图像周围出现白色条纹。

删除此填充关闭cardPreventCornerOverlap

  • 通过XML通过card_view:cardPreventCornerOverlap="false"

  • 编程通过yourCardView.setPreventCornerOverlap(false):你可以做到这一点。

请记住,这会给你只有API 20+的效果。 在具有较旧版本系统的设备上,您的圆角将不可见(隐藏在非圆形图像下)。

了解更多检查the CardView doc。在第一段中,您可以找到关于您的问题的官方说明。