简体   繁体   English

Android RecyclerView平台之间的间距差异

[英]Android RecyclerView Spacing Differences between Platforms

Disclosure up front, this is a school project. 提前披露,这是一个学校项目。

I've run into a somewhat confusing-to-me layout issue regarding a list of CardViews inside of a RecyclerView, that being that the spacing between cards is non-existent on 5.0, whereas it's fine on 4.4. 关于RecyclerView内的CardView列表,我遇到了一个令人困惑的布局问题,即在5.0上不存在卡之间的间距,而在4.4上则很好。 Here's two screenshots to demonstrate. 这是两个截图。

Android 4.4 (On Device, Galaxy S4) Android 4.4(在设备上,Galaxy S4) 4.4

Android 5.0.1 (On Device, Nexus 4) Android 5.0.1(在设备上,Nexus 4) 5.0.1

I can see the card corners in the 5.x version, so the cards are there. 我可以看到5.x版本中的卡角,因此卡就在那里。 I have a feeling this is something to do with some XML attribute that I need for running 5.x that the support library fakes automatically on 4.x, but googling has failed me. 我感觉这与运行5.x所需的一些XML属性有关,支持库在4.x上自动伪造了XML属性,但是使用Google搜索时失败了。 Here's the relevant XML: 这是相关的XML:

Fragment Layout 片段布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hinodesoftworks.kanagt.HiraganaListFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/hira_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</FrameLayout>

Card Layout 卡布局

<?xml version="1.0" encoding="utf-8"?>
<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:layout_height="match_parent"
    android:layout_width="match_parent"
    card_view:cardCornerRadius="4dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/kana_card_display"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="46sp"
            android:text="あ"/>

        <TextView
            android:id="@+id/kana_card_roma"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/kana_card_display"
            android:layout_toEndOf="@id/kana_card_display"
            android:text="a"
            />

        <TextView
            android:id="@+id/kana_card_info1_display"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/kana_card_display"
            android:layout_toEndOf="@id/kana_card_display"
            android:layout_below="@id/kana_card_roma"
            android:text="INFO 1"/>

        <TextView
            android:id="@+id/kana_card_info2_display"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/kana_card_display"
            android:layout_toEndOf="@id/kana_card_display"
            android:layout_below="@id/kana_card_info1_display"
            android:text="INFO 2"/>

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/kana_card_info2_display">
            <ImageView
                android:id="@+id/kana_card_diagram"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:scaleType="fitStart"

                android:adjustViewBounds="true"/>
    </HorizontalScrollView>
    </RelativeLayout>

</android.support.v7.widget.CardView>

I'm really hoping this is a dumb mistake with a simple XML fix; 我真的希望通过简单的XML修复是一个愚蠢的错误。 that the support library version is working fine makes me hopeful of that. 支持库版本运行良好,这使我充满希望。

If you want the padding on 5.0 follow the same rules as on previous platforms add this attribute to the CardView XML element 如果您希望5.0上的填充遵循与以前平台相同的规则,请将此属性添加到CardView XML元素

app:cardUseCompatPadding="true"

On Lollipop the shadows can be drawn outside of the view bounds. 在棒棒糖上,阴影可以绘制在视图范围之外。 On Kitkat and lower extra space has to be reserved for the shadows. 在Kitkat和更低的地方,必须为阴影保留额外的空间。 That's why on Lollipop the spacing would have to be set explicitly. 这就是为什么在Lollipop上必须明确设置间距的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM