android - Android Support Library的CardView在不同设备上的不同结果

标签 android android-layout android-support-library android-cardview

使用 Android 支持库 v7 小部件 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>

我得到了这些结果:

连结 4 (5.0.1): enter image description here

三星 Galaxy S4 (4.4.2): enter image description here

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

我错过了什么吗?

最佳答案

你所有的观察都是正确的:)
官方解释的都很好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.

和:

Note that, if you specify exact dimensions for the CardView, because of the shadows, its content area will be different between platforms before L and after L. By using api version specific resource values, you can avoid these changes. Alternatively, If you want CardView to add inner padding on platforms L and after as well, you can set setUseCompatPadding(boolean) to true.

如那里所述 - 您应该只使用 setUseCompatPadding (true)然后两者的外部填充:Lpre-L 将相同。

关于android - Android Support Library的CardView在不同设备上的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28153784/

相关文章:

Android项目提示 "The type bolts.Task cannot be resolved. It is indirectly referenced from required .class files"

android - 约束布局 : Compound ImageView that stays in parent when text expands

android - 创建透视(android)应用程序图标

android - 如何降低我的应用程序的内存使用量?

android - 使用 OpenCV 检测最大轮廓

安卓 : ScrollView stretches background image

android - GridView 行重叠 : how to make row height fit the tallest item?

Android 常见的 Activity 和 FragmentActivity 父类(super class)

android - 以编程方式将项目添加到相对布局

android设计库gradle空指针异常