简体   繁体   English

如何在 RecyclerView 中填充整个屏幕

[英]How to fill the entire screen in a RecyclerView

I have a problem filling the entire screen in a reclycle view.我在循环视图中填充整个屏幕时遇到问题。

This is my xml code of the cardview and my view where the reclyclerview is:这是我的卡片视图的 xml 代码和我的视图,reclyclerview 是:

CardView:卡片视图:

This is how it looks in the Ide Android studio:这是它在 Ide Android 工作室中的外观:

Layout where the Reclycler view is: Recycler 视图所在的布局:

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

    <TextView
        android:id="@+id/textAmount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="4dp"
        android:text="30"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/textIdPerson"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="4dp"
        android:text="30"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/textId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="4dp"
        android:text="30"
        android:textSize="30sp" />

</LinearLayout>






<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="4dp">
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        app:cardCornerRadius="4dp">

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/textAmount"
                android:padding="8dp"
                android:text="ID"
                android:textSize="30sp"
                android:layout_weight="1"
                android:gravity="center"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/textIdPerson"
                android:padding="8dp"
                android:layout_weight="1"
                android:text="UserId"
                android:textSize="30sp"
                android:gravity="center"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/textId"
                android:padding="8dp"
                android:textSize="30sp"
                android:layout_weight="1"
                android:text="Amount"
                android:gravity="center"/>

        </LinearLayout>


    </androidx.cardview.widget.CardView>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_gravity="center"

        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/recycler_view"
        android:nestedScrollingEnabled="false"
        tools:listitem="@layout/list_row_main"/>
    


</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_alignParentBottom="true">


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="atras"
        android:onClick="@{viewmodel::back}"
        android:layout_weight="1"
        android:layout_marginRight="4dp"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Siguiente"
        android:onClick="@{viewmodel::next}"
        android:layout_weight="1"/>
</LinearLayout>

Image: enter image description here图片:在此处输入图片描述

But when I start the application on the device it looks like this但是当我在设备上启动应用程序时,它看起来像这样

enter image description here在此处输入图像描述

Check your adapter class, Layout you designed for adapter class should have width match parent and three different textviews with equal width to hold values.检查您的适配器 class,您为适配器 class 设计的布局应该具有宽度匹配父级和三个具有相同宽度的不同文本视图以保存值。

Make sure the RecyclerView has layout_width also set to match_parent .确保RecyclerViewlayout_width也设置为match_parent

Your activity_main.xml(or any layout that hosts recyclerview) should look like this.您的 activity_main.xml(或任何托管 recyclerview 的布局)应如下所示。

<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_marginStart="5dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="5dp"
    app:cardCornerRadius="5dp"
    app:cardElevation="8dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:gravity="center_vertical|center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textAmount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="4dp"
            android:text="Amount"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/textIdPerson"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="4dp"
            android:text="UserID"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/textId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="4dp"
            android:text="Id"
            android:textSize="30sp" />
    </LinearLayout>
</androidx.cardview.widget.CardView>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center"
    android:layout_marginBottom="8dp"
    android:layout_weight="2"
    android:nestedScrollingEnabled="false"
    tools:listitem="@layout/list_item" />

<androidx.appcompat.widget.LinearLayoutCompat
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btnAtras"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        android:layout_weight="1"
        android:text="ATRAS"

        />

    <Button
        android:id="@+id/btnSignature"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        android:layout_weight="1"
        android:text="Signature" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>

And list_item.xml layout should be something like below:并且 list_item.xml 布局应该如下所示:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.cardview.widget.CardView
    android:layout_width="0dp"
    android:layout_height="60dp"
    app:cardCornerRadius="5dp"
    app:cardElevation="8dp"
    android:layout_marginStart="5dp"
    android:layout_marginEnd="5dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    app:cardUseCompatPadding="false"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_gravity="center"
        android:gravity="center|center_vertical"
        >
        <TextView
            android:gravity="center"
            android:id="@+id/textAmount"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:padding="8dp"
            android:text="Amount"
            android:textSize="30sp" />

        <TextView
            android:gravity="center"
            android:id="@+id/textIdPerson"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:padding="8dp"
            android:text="UserId"
            android:textSize="30sp" />

        <TextView
            android:gravity="center"
            android:id="@+id/textId"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:padding="8dp"
            android:text="Id"
            android:textSize="30sp" />
    </LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

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

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