Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

android - Unnecessary padding in 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.

It appears like this: screenshot_2014-12-27-20-31-55

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>

NOTE: The screenshot is captured on a pre-lollipop device.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I went through the developer docs again and found that:

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.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...