如何在Android L中设置CardView小部件的填充?

时间:2022-11-25 14:13:27

I'm using android:paddingLeft and android:paddingTop to set the padding for the new CardView widget but it doesn't work.

我使用的是android:用鼠标左键和android操作:在顶部设置为新的CardView小部件的填充,但它不起作用。

I can set the margin for all the controls inside the CardView as a workaround but that's a pain if there are too many controls.

我可以将CardView内所有控件的边距设置为一个工作区,但如果有太多控件,那就太痛苦了。

How to set padding for the new cardview widget?

如何为新的cardview小部件设置填充?

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:paddingLeft="20dp"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="20dp"
    android:paddingBottom="@dimen/activity_vertical_margin"
    card_view:cardCornerRadius="2dp">

    <TextView
        android:id="@+id/info_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>
</android.support.v7.widget.CardView>

4 个解决方案

#1


53  

CardView prior to L-preview uses RoundRectDrawableWithShadow to draw its background, which overrides Drawable.getPadding() to add shadow padding. The view background gets set via code after inflation, which overrides any padding specified in XML.

在l预览之前的CardView使用RoundRectDrawableWithShadow来绘制它的背景,它覆盖了Drawable.getPadding()来添加阴影填充。视图后台通过代码在膨胀后设置,它覆盖了XML中指定的任何填充。

You have two options:

你有两个选择:

  1. Set padding at run time using View.setPadding() and be careful to adjust for the shadows (but only prior to L-preview!).
  2. 在运行时使用View.setPadding()设置填充,并小心地调整阴影(但只有在l预览之前)。
  3. Place everything inside a layout that specifies padding.
  4. 将所有内容放置在指定填充的布局中。

The latter option is safest.

后一种选择是最安全的。

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    card_view:cardCornerRadius="2dp">

    <FrameLayout
        android:paddingLeft="20dp"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="20dp"
        android:paddingBottom="@dimen/activity_vertical_margin">

        <TextView
            android:id="@+id/info_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello World!"/>
    </FrameLayout>
</android.support.v7.widget.CardView>

#2


140  

CardView should handle this using the contentPadding attributes it comes with:

CardView应该使用它附带的contentPadding属性来处理这个问题:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    card_view:cardCornerRadius="2dp"
    card_view:contentPaddingLeft="20dp"
    card_view:contentPaddingRight="@dimen/activity_horizontal_margin"
    card_view:contentPaddingTop="20dp"
    card_view:contentPaddingBottom="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/info_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>
</android.support.v7.widget.CardView>

#3


24  

If you want to use CardView padding on pre-L devices, and have it look the same on Lollipop+ devices, then you will need to use setUseCompatPadding(true), or the XML variant cardUseCompatPadding="true".

如果您想要在pre-L设备上使用CardView padding,并且在Lollipop+设备上看起来是一样的,那么您需要使用setUseCompatPadding(true),或者使用XML变体cardUseCompatPadding="true"。

This is because "CardView adds additional padding to draw shadows on platforms before L."[1] So, the default implementation has the different API versions looking different and views may not line up properly. So, the easiest way to fix that issue is the ways stated above, or use margins instead.

这是因为“CardView添加了额外的填充,在l之前在平台上绘制阴影。”[1]因此,默认的实现有不同的API版本,看起来不同,视图可能不正确地对齐。因此,解决这个问题的最简单方法就是上面提到的方法,或者使用空白。

Example Code

<android.support.v7.widget.CardView
    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="wrap_context"
    card_view:cardUseCompatPadding="true"
    card_view:contentPadding="8dp"
    card_view:cardCornerRadius="4dp" >

    ...

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

Sources

[1] CardView.setUseCompatPadding(boolean)

[1]CardView.setUseCompatPadding(布尔)

[2] android.support.v7.cardview:cardUseCompatPadding

[2]android.support.v7.cardview:cardUseCompatPadding

#4


0  

This is what worked for me - putting every item in a frame layout

这就是我的工作——把每一个项目都放在一个框架布局中。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
 android:layout_marginBottom="-4dp">

   <android.support.v7.widget.CardView   
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"

            android:background="@color/white_three"
            android:orientation="vertical"
            card_view:cardCornerRadius="2dp"
            card_view:cardElevation="@dimen/card_elevation"
            card_view:cardMaxElevation="0dp"
            card_view:cardPreventCornerOverlap="false"
            card_view:cardUseCompatPadding="true"
   </android.support.v7.widget.CardView>

Double check that card_view is "http://schemas.android.com/apk/res-auto" and not tools, and also set negative margins on the frame view to maintain shadows - works fine.

双重检查card_view是“http://schemas.android.com/apk/res-auto”而不是工具,并且在框架视图上设置负的边距以保持阴影效果。

#1


53  

CardView prior to L-preview uses RoundRectDrawableWithShadow to draw its background, which overrides Drawable.getPadding() to add shadow padding. The view background gets set via code after inflation, which overrides any padding specified in XML.

在l预览之前的CardView使用RoundRectDrawableWithShadow来绘制它的背景,它覆盖了Drawable.getPadding()来添加阴影填充。视图后台通过代码在膨胀后设置,它覆盖了XML中指定的任何填充。

You have two options:

你有两个选择:

  1. Set padding at run time using View.setPadding() and be careful to adjust for the shadows (but only prior to L-preview!).
  2. 在运行时使用View.setPadding()设置填充,并小心地调整阴影(但只有在l预览之前)。
  3. Place everything inside a layout that specifies padding.
  4. 将所有内容放置在指定填充的布局中。

The latter option is safest.

后一种选择是最安全的。

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    card_view:cardCornerRadius="2dp">

    <FrameLayout
        android:paddingLeft="20dp"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="20dp"
        android:paddingBottom="@dimen/activity_vertical_margin">

        <TextView
            android:id="@+id/info_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello World!"/>
    </FrameLayout>
</android.support.v7.widget.CardView>

#2


140  

CardView should handle this using the contentPadding attributes it comes with:

CardView应该使用它附带的contentPadding属性来处理这个问题:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    card_view:cardCornerRadius="2dp"
    card_view:contentPaddingLeft="20dp"
    card_view:contentPaddingRight="@dimen/activity_horizontal_margin"
    card_view:contentPaddingTop="20dp"
    card_view:contentPaddingBottom="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/info_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>
</android.support.v7.widget.CardView>

#3


24  

If you want to use CardView padding on pre-L devices, and have it look the same on Lollipop+ devices, then you will need to use setUseCompatPadding(true), or the XML variant cardUseCompatPadding="true".

如果您想要在pre-L设备上使用CardView padding,并且在Lollipop+设备上看起来是一样的,那么您需要使用setUseCompatPadding(true),或者使用XML变体cardUseCompatPadding="true"。

This is because "CardView adds additional padding to draw shadows on platforms before L."[1] So, the default implementation has the different API versions looking different and views may not line up properly. So, the easiest way to fix that issue is the ways stated above, or use margins instead.

这是因为“CardView添加了额外的填充,在l之前在平台上绘制阴影。”[1]因此,默认的实现有不同的API版本,看起来不同,视图可能不正确地对齐。因此,解决这个问题的最简单方法就是上面提到的方法,或者使用空白。

Example Code

<android.support.v7.widget.CardView
    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="wrap_context"
    card_view:cardUseCompatPadding="true"
    card_view:contentPadding="8dp"
    card_view:cardCornerRadius="4dp" >

    ...

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

Sources

[1] CardView.setUseCompatPadding(boolean)

[1]CardView.setUseCompatPadding(布尔)

[2] android.support.v7.cardview:cardUseCompatPadding

[2]android.support.v7.cardview:cardUseCompatPadding

#4


0  

This is what worked for me - putting every item in a frame layout

这就是我的工作——把每一个项目都放在一个框架布局中。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
 android:layout_marginBottom="-4dp">

   <android.support.v7.widget.CardView   
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"

            android:background="@color/white_three"
            android:orientation="vertical"
            card_view:cardCornerRadius="2dp"
            card_view:cardElevation="@dimen/card_elevation"
            card_view:cardMaxElevation="0dp"
            card_view:cardPreventCornerOverlap="false"
            card_view:cardUseCompatPadding="true"
   </android.support.v7.widget.CardView>

Double check that card_view is "http://schemas.android.com/apk/res-auto" and not tools, and also set negative margins on the frame view to maintain shadows - works fine.

双重检查card_view是“http://schemas.android.com/apk/res-auto”而不是工具,并且在框架视图上设置负的边距以保持阴影效果。