RecyclerView中的项目不占用整个宽度

时间:2017-10-16 14:21:56

标签: android android-layout android-recyclerview

我是android开发的新手我做了一个recyclerview但是我在使用RecyclerView时有问题,一个项目没有占据屏幕的全宽我不知道是什么问题 这是问题的屏幕截图 enter image description here

这里是mainActivity.xml mainActivity.xml是包含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>

2 个答案:

答案 0 :(得分:2)

不要将指定的dp用于layout_heightlayout_width。您必须将recyclerview的宽度设置为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" />

答案 1 :(得分:0)

固定措施是一个糟糕的实践。 在您的主要活动中,请更改为:

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