Android支持Cardview contentPadding无法正常工作

我想在我的支持cardview中添加一个contentPadding.此填充是在4.4设备上正确计算的,但在4.1.2设备上,内容缓冲区不起作用.

的build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.android.support:cardview-v7:21.0.3'
}

XML布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

<android.support.v7.widget.CardView
    android:id="@+id/card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    card_view:cardCornerRadius="4dp"
    card_view:contentPaddingLeft="32dp"
    card_view:contentPaddingRight="32dp"
    card_view:contentPaddingTop="32dp"
    card_view:contentPaddingBottom="32dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is a very long text with more blablablablablablablablabla bla bla bla blablablablabla bla blablablablablabla blablablablabla blablablablabla blablabla"/>

    </LinearLayout>

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

</RelativeLayout>

设备运行4.1.2的结果:

contentPadding无法识别.

运行4.4.2设备的相同应用:

contentPadding被正确识别.

我在这个帖子上找到了解决这个问题的方法:How to set the padding for CardView widget in Android L
但我想知道,如果其他人正面临这个问题或者能提供更好的解决方案吗?

编辑:
目前,它似乎只是运行Android 4.1.2的Sony XPERIA L设备上的一个错误.我测试了运行4.1.2的Galaxy Nexus,在这种情况下,一切看起来都不错.

最佳答案 使用setUseCompatPadding(true)或XML参数app:cardUseCompatPadding =“true”

点赞