使列表项目大小一致

时间:2017-09-15 22:36:34

标签: android

我遇到的问题是,列表项的大小会因手机是否超过21而有所不同。

如何才能使21岁以前的设备之间的间距相同?

列表:

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:clipToPadding="false"
    android:divider="@color/transparent"
    android:dividerHeight="0dp"
    android:listSelector="@drawable/list_selector_transparent"
    android:paddingBottom="1dp"
    android:paddingTop="3dp"
    android:scrollbarAlwaysDrawVerticalTrack="false"
    android:smoothScrollbar="true"
    android:visibility="visible" />

档案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false">

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="3.5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="1.5dp"
    card_view:cardCornerRadius="3dp"
    card_view:cardElevation="1.7dp">
</android.support.v7.widget.CardView>
</LinearLayout>

如果您知道解决方案,我将非常感激。感谢

这是我的意思的图片:

Picture of the issue

1 个答案:

答案 0 :(得分:1)

由于在不同API级别上绘制阴影的方式,这种情况正在发生。在21+中,阴影在外部在视图本身中绘制,而在较旧的级别上,它必须在内部中绘制视图的边界。支持库在这些较旧的API级别上向CardView添加填充,以便有空间绘制阴影。

您可以将此属性添加到<android.support.v7.widget.CardView>标记,以使其行为相同,无论API级别如何:

app:cardUseCompatPadding="true"

来自the documentation

  

在Lollipop之前,CardView会在其内容中添加填充并为该区域绘制阴影。

  

或者,如果您希望CardView在Lollipop平台上添加内部填充,之后也可以调用setUseCompatPadding(boolean)并传递true。