Android CardView 删除填充 [英] Android CardView remove padding

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

问题描述

如何在下面的布局中摆脱这种奇怪的填充:

how do I get rid of this strange padding in the layout below:

<?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:background="@color/ColorPrimaryDark">

    <android.support.v7.widget.CardView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="10dp"
        android:layout_marginTop="9dp"
        android:layout_marginLeft="9dp"
        android:layout_marginRight="9dp"
        card_view:cardElevation="0.01dp"
        android:layout_marginBottom="0dp"
        card_view:cardPreventCornerOverlap="true"
        card_view:cardUseCompatPadding="true"
        card_view:cardBackgroundColor="@color/ColorPrimary">

        <RelativeLayout
            android:id="@+id/top_layout"
            android:layout_width="fill_parent"
            android:layout_height="180dp"
            android:background="@color/ColorPrimary">

            <ImageView
                android:id="@+id/img_thumbnail"
                android:layout_width="fill_parent"
                android:layout_height="180dp"
                android:scaleType="fitXY"
                android:background="@drawable/korabltest"/>

            <RelativeLayout
                android:id="@+id/inner_layout"
                android:layout_width="fill_parent"
                android:layout_height="36dp"
                android:background="#5c1b1b1b"
                android:layout_gravity="bottom"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true">

                <TextView
                    android:id="@+id/tv_nature"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:paddingLeft="5dp"
                    android:paddingRight="5dp"
                    android:layout_gravity="bottom"
                    android:gravity="center_vertical"
                    android:alpha="0.8"
                    android:textColor="#fff"
                    android:textSize="18sp"
                    android:text="Lexington"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <TextView
                    android:id="@+id/tv_nature_1"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:paddingLeft="5dp"
                    android:paddingRight="5dp"
                    android:layout_gravity="bottom"
                    android:gravity="center_vertical"
                    android:alpha="0.8"
                    android:textColor="#fff"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    android:text="527 (31%)"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true" />
            </RelativeLayout>
        </RelativeLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

推荐答案

UDPATE

好吧,似乎有一种更简单的方法可以做到这一点,而无需猜测填充和所有内容:

Well, seems there is a much easier way to do it, without guessing the padding and all:

card_view:cardPreventCornerOverlap="false"

或使用java:

cardView.setPreventCornerOverlap(false)

您可以在这里阅读所有相关内容.

原答案

这是一种有意的填充,以避免内容从棒棒糖前版本中的卡片圆角溢出(因为剪辑不可用).如果你想一起摆脱它,你可以在棒棒糖之前的版本中为 CardViewcontentPadding 属性使用负填充,例如:

It is an intentional padding to avoid content from bleeding off the rounded corner of the card in pre lollipop versions (since clipping is not available). If you want to get rid of it all together you may use a negative padding in pre-lollipop versions for the contentPadding attribute of the CardView such as:

card_view:contentPadding="-8dp"

或使用java:

int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
cardView.setContentPadding(-padding,-padding,-padding,-padding);

我不是特别确定哪个值会无缝运行,您需要自己进行试验并查看.

I'm not particularly sure which value will work seamlessly, you'll need to experiment and see for yourself.

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

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