【问题标题】:How to fill the entire screen in a RecyclerView如何在 RecyclerView 中填充整个屏幕
【发布时间】:2022-01-01 09:56:05
【问题描述】:

在循环视图中填充整个屏幕时遇到问题。

这是我的卡片视图的 xml 代码和我的 recyclerview 所在的视图:

卡片视图:

这是它在 Ide Android 工作室中的外观:

Reclyler 视图所在的布局:

<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>

图片: enter image description here

但是当我在设备上启动应用程序时,它看起来像这样

enter image description here

【问题讨论】:

    标签: java android xml android-studio android-room


    【解决方案1】:

    您的 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>
    

    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>
    

    【讨论】:

      【解决方案2】:

      检查您的适配器类,您为适配器类设计的布局应该具有宽度匹配父级和三个具有相同宽度的不同文本视图来保存值。

      【讨论】:

        【解决方案3】:

        确保RecyclerViewlayout_width 也设置为match_parent

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-07-29
          • 1970-01-01
          • 1970-01-01
          • 2021-04-05
          • 2015-06-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多