Android支持库的CardView在不同设备上的不同结果

GoR*_*RoS 5 android android-layout android-support-library android-cardview

使用Android Support Libraryv7小部件CardView我看到Galaxy S4与Nexus 4设备相比有不同的结果.具有以下布局:

<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:id="@+id/card_view"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:layout_gravity="center"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:orientation="horizontal"
    card_view:cardCornerRadius="7dp"
    card_view:cardElevation="12dp">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true">

        <TextView
            android:id="@+id/txtExample"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/no_messages" />

    </ScrollView>

</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

我得到了这些结果:

Nexus 4(5.0.1): 在此输入图像描述

三星Galaxy S4(4.4.2): 在此输入图像描述

似乎Nexus上的那个用它的边缘计算视图,然后在外面绘制阴影.另一方面,三星之一似乎应用了边距,然后在内部绘制阴影,直到达到计算出的View界限.

我错过了什么吗?

Mac*_*ęga 8

All your observations are correct :)
Everything is explained well on official documentation of CardView:

Before L, CardView adds padding to its content and draws shadows to that area. This padding amount is equal to maxCardElevation + (1 - cos45)*cornerRadius on the sides and maxCardElevation*1.5 + (1 - cos45)*cornerRadius on top and bottom.

and:

请注意,如果您为CardView指定了精确的尺寸,由于阴影,其内容区域在L之前和L之后的平台之间会有所不同.通过使用api版本特定的资源值,您可以避免这些更改.或者,如果您希望CardView在平台L和之后添加内部填充,您可以将setUseCompatPadding(boolean)设置为true.

如上所述 - 你应该只使用setUseCompatPadding (true)两个外部填充:Lpre-L将是相同的.