CardView 中不必要的填充? [英] Unnecessary padding in CardView?

查看:53
本文介绍了CardView 中不必要的填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的应用程序中实现了 CardView 并且一切正常,除了如果我将半径放置到卡上,图像周围会有一点填充.

I have implemented CardView in my app and everything works fine except there is a little padding around the image if I put radius to the card.

看起来像这样:

但是在 android 文档这篇文章 图像占据了整个cardview,所以你能帮我实现那个吗.

But in android docs and in this article the image takes the entire cardview, so can u help me achieve that.

我的布局文件是这样的:

My layout file is like:

<?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="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:padding="8dp">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    card_view:cardBackgroundColor="@android:color/white"
    card_view:cardCornerRadius="4dp">

    <ImageView
        android:id="@+id/media_image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:alpha="0.8"
            android:background="?attr/colorPrimary"
            android:padding="4dp">

            <TextView
                android:id="@+id/media_download"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:textSize="11sp"/>

            <TextView
                android:id="@+id/category_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:textColor="@color/primary_text"
                android:textSize="12sp"/>

        </RelativeLayout>

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

</LinearLayout>

注意:屏幕截图是在棒棒糖之前的设备上捕获的.

推荐答案

我浏览了 开发者文档 再次发现:

I went through the developer docs again and found that:

在 API 20 及之前版本中,CardView 不会为圆角裁剪 Card 的边界.相反,它会向内容添加填充,使其不会与圆角重叠.

On API 20 and before, CardView does not clip the bounds of the Card for the rounded corners. Instead, it adds padding to content so that it won't overlap with the rounded corners.

所以对于棒棒糖之前的设备,我必须在 cardview 上调用 setPreventCornerOverlap (false);.

So for pre-lollipop devices I have to call setPreventCornerOverlap (false); on the cardview.

更新:同样可以通过在卡片视图中添加 app:cardPreventCornerOverlap="false" 的 xml 代码来完成.

Update: The same can be done through xml code by adding app:cardPreventCornerOverlap="false" in card view.

这篇关于CardView 中不必要的填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆