有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

RelativeLayout中的java Android RecyclerView未正确滚动

我正在制作一个安卓应用程序,它有一个活动,用户可以选择要生成的随机数,然后显示这些数字。这都是在一个RelativelLayout内,而这个ScrollView内有一个ScrollView。在滚动视图中,thers是一个静态CardView,用户在其中选择所需的数字,然后单击一个按钮,该按钮将生成并显示在RecyclerView中(动态创建用户选择的相同数量的CardView)在静态CardView下。除了动态添加的CardView之间的滚动和间距之外,我所有的操作都正常。在Recylerview中的CardViews以某种方式被间隔得非常大

以下是该问题的屏幕截图:

Below is a screenshot of the issue:

My CardView项目布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:card_view="http://schemas.安卓.com/apk/res-auto"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent">

    <安卓.support.v7.widget.CardView
        安卓:id="@+id/LottoNumberGenerator_Numbers"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_margin="5dp"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="10dp">

        <RelativeLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content">

            <TextView
                安卓:id="@+id/LottoNumberGenerator_NumbersHeading"
                安卓:layout_width="match_parent"
                安卓:layout_height="wrap_content"
                安卓:layout_toStartOf="@+id/LottoNumberGenerator_Menu"
                安卓:gravity="center_horizontal"
                安卓:text="Number Combination #1"
                安卓:textAppearance="@style/TextAppearance.AppCompat.Large"
                安卓:textColor="@color/colorPrimary"
                安卓:textStyle="bold" />

            <ImageView
                安卓:id="@+id/LottoNumberGenerator_Menu"
                安卓:layout_width="20dp"
                安卓:layout_height="30dp"
                安卓:layout_alignParentRight="true"
                安卓:layout_margin="5dp"
                安卓:scaleType="centerCrop"
                安卓:src="@drawable/ic_dots"/>

            <TableLayout
                安卓:layout_width="match_parent"
                安卓:layout_height="wrap_content"
                安卓:layout_below="@+id/LottoNumberGenerator_Menu">

                <TableRow
                    安卓:layout_width="match_parent"
                    安卓:layout_height="match_parent">

                    <TextView
                        安卓:id="@+id/LottoNumberGenerator_Number1"
                        安卓:layout_width="0dp"
                        安卓:layout_height="50dp"
                        安卓:layout_margin="2dp"
                        安卓:layout_weight="1"
                        安卓:background="@drawable/number_red"
                        安卓:gravity="center"
                        安卓:text="10"
                        安卓:textSize="24sp"
                        安卓:textStyle="bold" />

                    <TextView
                        安卓:id="@+id/LottoNumberGenerator_Number2"
                        安卓:layout_width="0dp"
                        安卓:layout_height="50dp"
                        安卓:layout_margin="2dp"
                        安卓:layout_weight="1"
                        安卓:background="@drawable/number_green"
                        安卓:gravity="center"
                        安卓:text="30"
                        安卓:textSize="24sp"
                        安卓:textStyle="bold" />

                    <TextView
                        安卓:id="@+id/LottoNumberGenerator_Number3"
                        安卓:layout_width="0dp"
                        安卓:layout_height="50dp"
                        安卓:layout_margin="2dp"
                        安卓:layout_weight="1"
                        安卓:background="@drawable/number_blue"
                        安卓:gravity="center"
                        安卓:text="50"
                        安卓:textSize="24sp"
                        安卓:textStyle="bold" />

                    <TextView
                        安卓:id="@+id/LottoNumberGenerator_Number4"
                        安卓:layout_width="0dp"
                        安卓:layout_height="50dp"
                        安卓:layout_margin="2dp"
                        安卓:layout_weight="1"
                        安卓:background="@drawable/number_yellow"
                        安卓:gravity="center"
                        安卓:text="70"
                        安卓:textSize="24sp"
                        安卓:textStyle="bold" />

                    <TextView
                        安卓:id="@+id/LottoNumberGenerator_Number5"
                        安卓:layout_width="0dp"
                        安卓:layout_height="50dp"
                        安卓:layout_margin="2dp"
                        安卓:layout_weight="1"
                        安卓:background="@drawable/number_red"
                        安卓:gravity="center"
                        安卓:text="90"
                        安卓:textSize="24sp"
                        安卓:textStyle="bold" />

                    <TextView
                        安卓:id="@+id/LottoNumberGenerator_Number6"
                        安卓:layout_width="0dp"
                        安卓:layout_height="50dp"
                        安卓:layout_margin="2dp"
                        安卓:layout_weight="1"
                        安卓:background="@drawable/number_yellow"
                        安卓:gravity="center"
                        安卓:text="99"
                        安卓:textSize="24sp"
                        安卓:textStyle="bold" />
                </TableRow>

            </TableLayout>

            <Space
                安卓:layout_width="1dp"
                安卓:layout_height="10dp" />

        </RelativeLayout>

    </安卓.support.v7.widget.CardView>

</RelativeLayout>

我的主要活动代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    xmlns:card_view="http://schemas.安卓.com/apk/res-auto"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_lotto_number_generator">

    <ScrollView
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:layout_above="@+id/Generator_BannerAd"
        安卓:layout_alignParentStart="true">

        <RelativeLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="match_parent"
            安卓:layout_margin="5dp"
            安卓:layout_marginBottom="10dp">

            <安卓.support.v7.widget.CardView
                安卓:id="@+id/Generator_Lines"
                安卓:layout_width="match_parent"
                安卓:layout_height="wrap_content"
                安卓:layout_gravity="center"
                安卓:layout_margin="5dp"
                card_view:cardCornerRadius="2dp"
                card_view:contentPadding="10dp">

                <RelativeLayout
                    安卓:layout_width="match_parent"
                    安卓:layout_height="wrap_content">

                    <TextView
                        安卓:id="@+id/Generator_LinesHeading"
                        安卓:layout_width="match_parent"
                        安卓:layout_height="wrap_content"
                        安卓:gravity="center_horizontal"
                        安卓:text="Numbers Wanted?"
                        安卓:textAppearance="@style/TextAppearance.AppCompat.Large"
                        安卓:textColor="@color/colorPrimary"
                        安卓:textStyle="bold" />

                    <TableLayout
                        安卓:layout_width="match_parent"
                        安卓:layout_height="wrap_content"
                        安卓:layout_below="@+id/Generator_LinesHeading">

                        <TableRow
                            安卓:layout_width="match_parent"
                            安卓:layout_height="match_parent">

                            <Button
                                安卓:id="@+id/Generator_LinesMinus"
                                安卓:layout_width="0dp"
                                安卓:layout_height="100dp"
                                安卓:layout_gravity="center_vertical|center_horizontal"
                                安卓:layout_margin="5dp"
                                安卓:layout_weight="1"
                                安卓:background="@drawable/button"
                                安卓:gravity="center_vertical"
                                安卓:padding="0dp"
                                安卓:text="-"
                                安卓:textAlignment="center"
                                安卓:textColor="@color/colorPrimaryDark"
                                安卓:textSize="55sp"
                                安卓:textStyle="bold" />

                            <TextView
                                安卓:id="@+id/Generator_LinesAmount"
                                安卓:layout_width="0dp"
                                安卓:layout_height="match_parent"
                                安卓:layout_margin="5dp"
                                安卓:layout_weight="1"
                                安卓:gravity="center_vertical|center_horizontal"
                                安卓:text="1"
                                安卓:textAlignment="center"
                                安卓:textColor="@color/colorPrimaryDark"
                                安卓:textSize="60sp"
                                安卓:textStyle="bold" />

                            <Button
                                安卓:id="@+id/Generator_LinsPlus"
                                安卓:layout_width="0dp"
                                安卓:layout_height="100dp"
                                安卓:layout_gravity="center_vertical|center_horizontal"
                                安卓:layout_margin="5dp"
                                安卓:layout_weight="1"
                                安卓:background="@drawable/button"
                                安卓:gravity="center_vertical"
                                安卓:padding="0dp"
                                安卓:text="+"
                                安卓:textAlignment="center"
                                安卓:textColor="@color/colorPrimaryDark"
                                安卓:textSize="55sp"
                                安卓:textStyle="bold" />

                        </TableRow>

                        <TableRow>

                            <Button
                                安卓:id="@+id/Generator_Generate"
                                安卓:layout_width="0dp"
                                安卓:layout_height="50dp"
                                安卓:layout_margin="5dp"
                                安卓:layout_weight="1"
                                安卓:background="@drawable/button"
                                安卓:text="Generate Numbers"
                                安卓:textColor="@color/colorPrimaryDark"
                                安卓:textSize="18sp" />

                        </TableRow>

                    </TableLayout>

                    <Space
                        安卓:layout_width="1dp"
                        安卓:layout_height="10dp" />

                </RelativeLayout>

            </安卓.support.v7.widget.CardView>

            <安卓.support.v7.widget.RecyclerView
                安卓:id="@+id/Generator_RecyclerView"
                安卓:layout_width="match_parent"
                安卓:layout_height="wrap_content"
                安卓:layout_below="@+id/Generator_Lines"
                安卓:scrollbars="none">

            </安卓.support.v7.widget.RecyclerView>

        </RelativeLayout>

    </ScrollView>

    <LinearLayout
        安卓:id="@+id/Generator_BannerAd"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_alignParentBottom="true"
        安卓:orientation="vertical"
        安卓:layout_marginTop="5dp"></LinearLayout>

</RelativeLayout>

共 (2) 个答案

  1. # 1 楼答案

    在项目布局的第6行中,替换

    android:layout_height="match_parent"
    

    android:layout_height="wrap_content"
    
  2. # 2 楼答案

    这可能很简单。我认为正在发生的是,由于CardView中的RelativeLayout的高度为match_parent,因此它将cardview的高度与整个布局的高度相匹配。如果您将其更改为wrap_content,它将解决您的问题