78

新しいウィジェットのパディングを設定するためにandroid:paddingLeftandを使用していますが、機能しません。android:paddingTopCardView

回避策として、内部のすべてのコントロールのマージンを設定できますが、コントロールCardViewが多すぎる場合は面倒です。

新しい 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

4 に答える 4

208

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>
于 2014-11-11T00:01:28.933 に答える
73

L-preview の前の CardView はRoundRectDrawableWithShadow、その背景を描画するために使用します。これDrawable.getPadding()は、シャドウ パディングを追加するためにオーバーライドされます。ビューの背景は、インフレーション後にコードを介して設定され、XML で指定されたパディングをオーバーライドします。

次の 2 つのオプションがあります。

  1. を使用して実行時にパディングを設定View.setPadding()し、シャドウを調整するように注意してください (ただし、L-プレビューの前にのみ!)。
  2. パディングを指定するレイアウト内にすべてを配置します。

後者のオプションが最も安全です。

<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>
于 2014-07-22T20:59:01.930 に答える
36

L より前のデバイスで CardView パディングを使用し、Lollipop+ デバイスでも同じように表示する場合はsetUseCompatPadding(true)、または XML バリアントを使用する必要がありますcardUseCompatPadding="true"

これは、「CardView は、L より前のプラットフォームで影を描画するために追加のパディングを追加する」ためです。したがって、その問題を修正する最も簡単な方法は、上記の方法を使用するか、代わりにマージンを使用することです。

サンプルコード

<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>

ソース

[1] CardView.setUseCompatPadding(ブール値)

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

于 2014-11-30T16:57:27.020 に答える