RecyclerView条目下方的大量空间

时间:2019-01-12 00:11:30

标签: android xml android-studio android-recyclerview fragment

我的每个RecyclerView条目下面都有一个很大的空间(整个屏幕其余部分几乎-不完整),我也不知道是什么原因引起的。我有一个带有重量的LinearLayout,但是它包含在另一个具有固定高度的LinearLayout中(如下代码所示)。这是我的EntryItem布局以及主机布局(Fragment):

条目项布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="675dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:weightSum="5"
    android:padding="16dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:weightSum="7">

        <android.support.v7.widget.CardView
            android:id="@+id/top_card"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="-10dp"
            android:layout_weight="2"
            app:cardBackgroundColor="@color/redditOrange"
            app:cardCornerRadius="10dp">

            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <android.support.constraint.Guideline
                    android:id="@+id/top_card_vert"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    app:layout_constraintGuide_percent="0.075" />

                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_marginLeft="4dp"
                    android:layout_marginTop="4dp"
                    app:srcCompat="@drawable/reddit"
                    app:layout_constraintRight_toLeftOf="@id/top_card_vert"/>

                <TextView
                    android:id="@+id/label"
                    android:layout_width="349dp"
                    android:layout_height="20dp"
                    android:layout_marginLeft="30dp"
                    android:layout_marginTop="4dp"
                    android:fontFamily="@font/volte_regular"
                    android:gravity="center_vertical"
                    android:text="r/tifu"
                    app:layout_constraintStart_toEndOf="@id/top_card_vert"
                    android:textColor="#ffffff" />

            </android.support.constraint.ConstraintLayout>

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

        <android.support.v7.widget.CardView
            android:id="@+id/main_card"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="5"
            app:cardBackgroundColor="@color/cardColor"
            app:cardElevation="7dp">

            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.constraint.Guideline
                    android:id="@+id/card_title_line"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    app:layout_constraintGuide_percent="0.2" />

                <TextView
                    android:paddingTop="2dp"
                    android:id="@+id/title"
                    android:layout_width="match_parent"
                    android:layout_height="20dp"
                    android:layout_marginLeft="4dp"
                    android:layout_marginTop="4dp"
                    android:fontFamily="@font/volte_bold"
                    android:text="TIFU by trying to buy a Christmas present for my wife"
                    app:layout_constraintBottom_toTopOf="@+id/card_title_line"
                    android:textColor="@color/offBlack" />


            </android.support.constraint.ConstraintLayout>

            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <!-- Using ConstraintLayouts to do percents are recommended instead of using nested weights (LinearLayouts) -->

                <android.support.constraint.Guideline
                    android:id="@+id/hor_guideline_below"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    app:layout_constraintGuide_begin="73dp" />

                <android.support.constraint.Guideline
                    android:id="@+id/above_text_content"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    app:layout_constraintGuide_percent="0.27" />

                <android.support.constraint.ConstraintLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <android.support.constraint.Guideline
                        android:id="@+id/ver_guideline_subtitle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        app:layout_constraintGuide_percent="0.32" />

                    <android.support.constraint.Guideline
                        android:id="@+id/hor_guideline_above"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        app:layout_constraintGuide_percent="0.38" />

                    <TextView
                        android:id="@+id/author"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/volte_regular"
                        android:gravity="center_vertical"
                        android:paddingTop="7dp"
                        android:paddingBottom="1dp"
                        android:text="/u/TheFiveHundred"
                        android:textColor="@color/darkRedditOrange"
                        android:textSize="10sp"
                        app:layout_constraintLeft_toRightOf="@id/ver_guideline_subtitle"
                        app:layout_constraintTop_toBottomOf="@id/hor_guideline_above" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:layout_constraintStart_toEndOf="@id/author"
                        app:layout_constraintEnd_toStartOf="@id/date_created"
                        app:layout_constraintLeft_toRightOf="@id/ver_guideline_subtitle"
                        app:layout_constraintTop_toBottomOf="@id/hor_guideline_above"
                        android:paddingTop="5dp"
                        android:textColor="@color/darkRedditOrange"
                        android:textSize="10sp"
                        android:text="|"/>

                    <TextView
                        android:id="@+id/date_created"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="116dp"
                        android:fontFamily="@font/volte_regular"
                        android:gravity="center_vertical"
                        android:paddingBottom="1dp"
                        android:paddingTop="7dp"
                        android:text="12.12.2018"
                        android:textColor="@color/darkRedditOrange"
                        android:textSize="10sp"
                        app:layout_constraintLeft_toRightOf="@id/ver_guideline_subtitle"
                        app:layout_constraintTop_toBottomOf="@id/hor_guideline_above" />
                </android.support.constraint.ConstraintLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:layout_gravity="bottom">

                    <ImageView
                        android:id="@+id/thumbnail"
                        android:layout_width="63dp"
                        android:layout_height="63dp"
                        android:layout_gravity="bottom"
                        android:layout_marginStart="4dp"
                        android:layout_marginBottom="3dp"
                        android:tint="@color/redditOrange"
                        app:srcCompat="@mipmap/ic_launcher" />

                    <TextView
                        android:id="@+id/textContent"
                        android:layout_width="304dp"
                        android:layout_height="63dp"
                        android:layout_gravity="bottom|end"
                        android:layout_marginEnd="8dp"
                        android:fontFamily="@font/volte_regular"
                        android:gravity="center_vertical"
                        android:paddingLeft="5dp"
                        android:text="@string/lorem_ipsum"
                        android:textColor="@color/darkGray"
                        android:textSize="12sp"
                        app:layout_constraintBottom_toTopOf="@+id/hor_guideline_below"
                        app:layout_constraintHorizontal_bias="0.0"
                        app:layout_constraintStart_toEndOf="@+id/thumbnail"
                        app:layout_constraintTop_toBottomOf="@id/above_text_content"
                        app:layout_constraintVertical_bias="0.0" />

                </LinearLayout>

            </android.support.constraint.ConstraintLayout>

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

</LinearLayout>

片段布局(主机)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

删除

android:weightSum="5"

来自父级LinearLayout。
另外,您是否需要此固定高度?:

android:layout_height="675dp"