items in RecyclerView doesn't take the full width

£可爱£侵袭症+ 提交于 2019-12-13 08:44:30

问题


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

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>

here's the 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>

回答1:


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



回答2:


Fixed measures are a bad pratice. in your main activity, change to:

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerId"
        android:layout_width="match_parent" <!--THIS LINE-->
        android:layout_height="match_parent"<!--THIS LINE or wrap_content-->
        tools:layout_editor_absoluteY="16dp"
        android:layout_marginRight="0dp"<!--why it was 8? obviously it wont work-->
        app:layout_constraintRight_toRightOf="parent" />


来源:https://stackoverflow.com/questions/46772596/items-in-recyclerview-doesnt-take-the-full-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!