【问题标题】:Android support Cardview contentPadding not workingAndroid 支持 Cardview contentPadding 不起作用
【发布时间】:2015-02-10 23:17:34
【问题描述】:

我想在我的支持卡片视图中添加一个 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 进行了测试,在这种情况下,一切看起来都不错。

【问题讨论】:

    标签: android android-cardview


    【解决方案1】:

    使用setUseCompatPadding(true),或XML参数app:cardUseCompatPadding="true"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 2021-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多