【问题标题】:Card View not proper in 5.1.1 Tablet and 5.0.2 Mobile Device5.1.1 平板电脑和 5.0.2 移动设备中的卡片视图不正确
【发布时间】:2016-05-19 06:42:03
【问题描述】:


我正在使用 CardView 创建以下视图。
在 gradle 依赖项中添加依赖项 compile 'com.android.support:cardview-v7:23.0.+'

下面是相同的xml文件。

<android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/cardViewEmp"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="0.50"
                card_view:cardCornerRadius="20dp"
                card_view:cardElevation="10dp"
                android:padding="@dimen/margin_10"
                >
                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    >
                <ImageView
                    android:id="@+id/employeeIcon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingTop="@dimen/margin_5"
                    android:src="@drawable/employeeicon"
                    android:layout_centerHorizontal="true"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Employee"
                    android:layout_centerHorizontal="true"
                    android:layout_below="@+id/employeeIcon"
                    android:textSize="@dimen/textSizeNormal"
                    />
                </RelativeLayout>
            </android.support.v7.widget.CardView>


            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/cardViewVehicle"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="0.50"
                card_view:cardCornerRadius="20dp"
                card_view:cardElevation="10dp"
                android:padding="@dimen/margin_10"
                >
                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    >
                    <ImageView
                        android:id="@+id/vehicleIconLive"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingTop="@dimen/margin_5"
                        android:src="@drawable/vehicleicon"
                        android:layout_centerHorizontal="true"
                        />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Vehicle"
                        android:layout_centerHorizontal="true"
                        android:layout_below="@+id/vehicleIconLive"
                        android:textSize="@dimen/textSizeNormal"
                        />
                </RelativeLayout>
                </android.support.v7.widget.CardView>

现在 4.4.4 移动版中视图的立面和外观如下所示,看起来不错!


但在 5.1.1 Nexus Tablet 和 5.0.2 Mobile 中看起来很奇怪,如下所示。

我通过这个添加了高程和圆角半径

card_view:cardCornerRadius="20dp"
card_view:cardElevation="10dp"
  • 如果没有适当的高度,任何人都可以清楚地看到拐角半径看起来很奇怪。
  • 可能是什么问题。
  • 如何使 CardView 具有正确的外观和感觉

编辑 1:
尝试使用 com.android.support:cardview-v7:23.1.1 ,但输出再次相同。

【问题讨论】:

  • 致电compile 'com.android.support:cardview-v7:23.1.1'
  • @IntelliJAmiya 我也试过了。在 5.1.1 Nexus 平板电脑中不起作用!!!

标签: android android-cardview cornerradius android-elevation


【解决方案1】:

尝试从卡片视图中删除:android:padding 属性。

来自文档:

由于填充用于偏移阴影的内容,因此您无法设置 CardView 上的填充。相反,您可以使用内容填充属性 在 XML 或 setContentPadding(int, int, int, int) 在代码中设置 Card 的边缘和 CardView 的子项之间的填充。

还有这个:

请注意,如果您指定 CardView 的确切尺寸,因为 阴影中,其内容区域会因平台而异 L 之前和 L 之后。通过使用 api 版本特定的资源值, 你可以避免这些变化。或者,如果您希望 CardView 在平台 L 和之后添加内部填充,您可以设置 setUseCompatPadding(boolean) 为 true。

【讨论】:

    【解决方案2】:

    我终于找到了问题所在。这是由于我写的外部LinearLayout 使屏幕中心的图标高度为wrap_content。我避开了LinearLayout,通过下面的代码,我用适当的高程曲线和中间制作了卡片视图。

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/mainRL"
            >
    <android.support.v4.widget.Space
                    android:layout_width="@dimen/margin_10"
                    android:layout_height="1dp"
                    android:layout_centerInParent="true"
                    android:layout_centerVertical="true"
                    android:id="@+id/spaceCenter"
                    />
    <!-- Employee icon -->
    
                <android.support.v7.widget.CardView
                    xmlns:card_view="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/cardViewEmp"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    card_view:cardCornerRadius="20dp"
                    card_view:cardElevation="10dp"
                    android:layout_toLeftOf="@+id/spaceCenter"
                    android:layout_centerInParent="true"
                    >
                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        >
                    <ImageView
                        android:id="@+id/employeeIcon"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingTop="@dimen/margin_5"
                        android:src="@drawable/employeeicon"
                        android:layout_centerHorizontal="true"
                        />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="  Employee  "
                        android:layout_centerHorizontal="true"
                        android:layout_below="@+id/employeeIcon"
                        android:textSize="@dimen/textSizeNormal"
                        />
                    </RelativeLayout>
                </android.support.v7.widget.CardView>
    
                <!-- Vehicle icon -->
    
                <android.support.v7.widget.CardView
                    xmlns:card_view="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/cardViewVehicle"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    card_view:cardCornerRadius="20dp"
                    card_view:cardElevation="10dp"
                    android:layout_toRightOf="@+id/spaceCenter"
                    android:layout_centerInParent="true"
                    >
                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        >
                        <ImageView
                            android:id="@+id/vehicleIconLive"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:paddingTop="@dimen/margin_5"
                            android:src="@drawable/vehicleicon"
                            android:layout_centerHorizontal="true"
                            />
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="    Vehicle     "
                            android:layout_centerHorizontal="true"
                            android:layout_below="@+id/vehicleIconLive"
                            android:textSize="@dimen/textSizeNormal"
                            />
                    </RelativeLayout>
                    </android.support.v7.widget.CardView>
    </RelativeLayout>
    

    【讨论】:

      【解决方案3】:

      要使其在 api 21 以上或以下兼容,您需要在支持 CardView 中指定 app:cardUseCompatPadding="true"。

      归功于 ShinChven CardView elevation not working on Android 5.1.1

      【讨论】:

        猜你喜欢
        • 2012-04-08
        • 1970-01-01
        • 1970-01-01
        • 2016-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-09
        • 2014-06-26
        相关资源
        最近更新 更多