RecyclerView中的项目不会占用全部宽度 [英] items in RecyclerView doesn't take the full width

查看:69
本文介绍了RecyclerView中的项目不会占用全部宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android开发的新手,我制作了一个recyclerview,但是在使用RecyclerView时遇到问题,一个项目无法占据屏幕的全宽度,我不知道这是什么问题 这是问题的屏幕截图

I am new in android development i make a recyclerview but I have a problem when using the RecyclerView , an item doesn't take the full width in the screen I don't know what's the problem here 's a screen shot of the problem

这是mainActivity.xml mainActivity.xml是包含recyclerview的布局

here's the mainActivity.xml mainActivity.xml is the layout that contain the recyclerview

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:alpha = "0.8"
    tools:context="com.example.abdelmagied.bakingapp.MainActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerId"
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteY="16dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>

这是recyclerItem.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="122dp"
    android:background="#999999"
    android:orientation="vertical">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="88dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:fontFamily="sans-serif"
        android:textColor = "#070707"
        android:text="Name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.888" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="57dp"
        android:textSize = "25dp"
        android:layout_marginTop="8dp"
        android:text="Nuttella Pie"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/textView"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.089" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:text="Servings"
        android:fontFamily="sans-serif"
        android:textColor = "#070707"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.49" />

    <TextView
        android:id="@+id/servings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:textSize = "25dp"
        android:layout_marginLeft="74dp"
        android:text="8"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/textView3"
        />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="204dp"
        android:layout_height="107dp"
        app:srcCompat="@drawable/chef"
        tools:layout_editor_absoluteY="8dp"
        android:layout_marginLeft="200dp"
        app:layout_constraintLeft_toLeftOf="parent" />
</android.support.constraint.ConstraintLayout>

推荐答案

请勿对layout_heightlayout_width使用指定的dp.您必须将recyclerview的宽度设置为match_parent

Don't use specified dp to layout_height and layout_width. You must set your recyclerview's width to match_parent

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerId"
    android:layout_width="match_parent" //This line
    android:layout_height="495dp"
    tools:layout_editor_absoluteY="16dp"
    android:layout_marginRight="8dp" //Also remove this line...
    app:layout_constraintRight_toRightOf="parent" />

这篇关于RecyclerView中的项目不会占用全部宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆