CardView has extra margin in each edge on Pre-Lollipop

前端 未结 7 762
闹比i
闹比i 2020-12-24 14:05

Here are two pictures.

on Lollipop: \"on

on Pre-Lollipop:

相关标签:
7条回答
  • 2020-12-24 14:47

    If the Android 4 (pre-lollipop) behavior is desired, adding app:cardUseCompatPadding="true" to the CardView should fix it.

    If the Android 5+ behavior is desired (which is actually the right behavior of card views according to material guidelines), the exact same thing can not be easily achieved. I usually use this fix to avoid defining multiple layout files and have reasonable output on all devices:

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:contentPaddingRight="@dimen/fix_cardview"
        app:contentPaddingLeft="@dimen/fix_cardview"
        app:contentPaddingTop="@dimen/fix_cardview_vertical"
        app:contentPaddingBottom="@dimen/fix_cardview_vertical" />
    

    and in normal values/dimens.xml file we should have:

    <dimen name="fix_cardview">-8dp</dimen>
    <dimen name="fix_cardview_vertical">-12dp</dimen>
    

    and in values-v21/dimens.xml:

    <dimen name="fix_cardview">0dp</dimen>
    <dimen name="fix_cardview_vertical">0dp</dimen>
    

    note that the numbers -8dp and -12dp might have to be adjusted for your layout, since they depend on elevation, etc.

    This is only a work-around to avoid ugly paddings in Android 4 views, without using different views in different layout files (which usually makes the code more difficult to maintain)

    0 讨论(0)
提交回复
热议问题