Android CardView 删除填充
Android CardView remove padding
如何去掉下面布局中的这个奇怪的填充:
<?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
好吧,似乎有一种更简单的方法,无需猜测填充和所有内容:
card_view:cardPreventCornerOverlap="false"
或使用java:
cardView.setPreventCornerOverlap(false)
您可以阅读所有相关内容 here。
原始答案
这是一种有意填充,以避免内容在棒棒糖之前的版本中从卡片的圆角渗出(因为剪辑不可用)。如果您想一起摆脱它,您可以在棒棒糖之前的版本中为 CardView
的 contentPadding
属性使用负填充,例如:
card_view:contentPadding="-8dp"
或使用java:
int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
cardView.setContentPadding(-padding,-padding,-padding,-padding);
我不是特别确定哪个值可以无缝工作,您需要自己试验看看。
遇到了同样的问题,this 对我有用。
app:cardPreventCornerOverlap="false"
对于 API < 21 的设备,我必须制作自定义 ImageView class,覆盖 onDraw 以绘制圆角。
@Override
protected void onDraw(Canvas canvas) {
float radius = getContext().getResources().getDimension(R.dimen.card_corner_radius);
Path path = new Path();
RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
path.addRoundRect(rect, radius, radius, Path.Direction.CW);
canvas.clipPath(path);
super.onDraw(canvas);
}
请从您的代码中删除此内容 -
card_view:cardUseCompatPadding="true"
使用这个:
cardView.setPadding(0,0,0,0);
cardView.setUseCompatPadding(true);
cardView.setContentPadding(-6,-6,-6,-6);
cardView.setPreventCornerOverlap(false);
我之前专门定义了cardview的高度-将其更改为“wrap_content”后,问题就解决了。
如何去掉下面布局中的这个奇怪的填充:
<?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
好吧,似乎有一种更简单的方法,无需猜测填充和所有内容:
card_view:cardPreventCornerOverlap="false"
或使用java:
cardView.setPreventCornerOverlap(false)
您可以阅读所有相关内容 here。
原始答案
这是一种有意填充,以避免内容在棒棒糖之前的版本中从卡片的圆角渗出(因为剪辑不可用)。如果您想一起摆脱它,您可以在棒棒糖之前的版本中为 CardView
的 contentPadding
属性使用负填充,例如:
card_view:contentPadding="-8dp"
或使用java:
int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
cardView.setContentPadding(-padding,-padding,-padding,-padding);
我不是特别确定哪个值可以无缝工作,您需要自己试验看看。
遇到了同样的问题,this 对我有用。
app:cardPreventCornerOverlap="false"
对于 API < 21 的设备,我必须制作自定义 ImageView class,覆盖 onDraw 以绘制圆角。
@Override
protected void onDraw(Canvas canvas) {
float radius = getContext().getResources().getDimension(R.dimen.card_corner_radius);
Path path = new Path();
RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
path.addRoundRect(rect, radius, radius, Path.Direction.CW);
canvas.clipPath(path);
super.onDraw(canvas);
}
请从您的代码中删除此内容 -
card_view:cardUseCompatPadding="true"
使用这个:
cardView.setPadding(0,0,0,0);
cardView.setUseCompatPadding(true);
cardView.setContentPadding(-6,-6,-6,-6);
cardView.setPreventCornerOverlap(false);
我之前专门定义了cardview的高度-将其更改为“wrap_content”后,问题就解决了。