为什么我的RecyclerView Layout宽度看起来像它的包装内容,即使我将其与父项匹配

时间:2018-11-09 04:24:52

标签: android android-studio android-layout

所以我有一个回收站视图布局,如下图所示。我将宽度设置为与父项匹配,并且在此布局中就像我想要的那样。因此,当我将此回收站布局称为我的家时,我希望视图完全像这样。  Recycler Layout

但是当我将回收站视图调用到我的主页布局中并尝试运行它时,为什么会变成这样?就像我的宽度是包装内容。直到右侧宽度才完整。

My recycler view when I run the app

这是我的recyclerview布局xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:layout_margin="5dp">

   <android.support.constraint.ConstraintLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:layout_marginLeft="5dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            android:src="@mipmap/ic_launcher"
            />

        <TextView
            android:id="@+id/textViewTitle"
            android:layout_marginLeft="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toRightOf="@+id/imageView"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="20dp"
            android:text="Plastik"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textViewCategory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            app:layout_constraintTop_toBottomOf="@+id/textViewTitle"
            app:layout_constraintLeft_toRightOf="@+id/imageView"
            android:text="Non Organik"
            android:textSize="16sp"/>

        <TextView
            android:id="@+id/textViewPrice"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Price"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginRight="5dp"
            android:textSize="@dimen/font_medium"
            android:textStyle="bold"/>

   </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>

这是我的首页布局xml代码

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.NestedScrollView 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:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:orientation="vertical"
    android:theme="@style/HomeTheme"
    tools:context=".HomeFragment">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <ImageView
            android:id="@+id/logog40w"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:src="@drawable/logog4w"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/pohong4w"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_alignParentRight="true"
            android:src="@drawable/pohong4w"
            app:layout_constraintRight_toRightOf="parent" />


        <TextView
            android:id="@+id/user_name"
            android:layout_width="85dp"
            android:layout_height="wrap_content"

            android:layout_marginTop="100dp"
            android:text="Name"
            android:textSize="25sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/pohong4w"
            app:layout_constraintTop_toTopOf="@+id/logog40w" />

        <TextView
            android:id="@+id/jemputrutin_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:text="Status Penjemputan Rutin:"
            android:textSize="18sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/pohong4w" />

        <TextView
            android:id="@+id/fragment_home_jemputrutin_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="STATUS"
            android:textSize="18sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toRightOf="@+id/jemputrutin_status"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="@+id/jemputrutin_status" />

        <LinearLayout

            android:id="@+id/layoutmainfeature"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"

            android:orientation="horizontal"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/jemputrutin_status">

            <android.support.v7.widget.CardView
                android:id="@+id/jemput_rutin"
                android:layout_width="105dp"
                android:layout_height="135dp"
                android:layout_margin="5dp"
                tools:layout_editor_absoluteX="51dp"
                tools:layout_editor_absoluteY="51dp">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:orientation="vertical"
                    android:padding="8dp"

                    >

                    <ImageView
                        android:layout_width="90dp"
                        android:layout_height="90dp"
                        android:padding="4dp"
                        android:src="@drawable/g4w_jemputrutin" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="Jemput Rutin"
                        android:textColor="#000000"
                        android:textSize="12dp" />
                </LinearLayout>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:id="@+id/antar_sendiri"
                android:layout_width="105dp"
                android:layout_height="135dp"
                android:layout_margin="5dp"
                tools:layout_editor_absoluteX="51dp"
                tools:layout_editor_absoluteY="51dp">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:orientation="vertical"
                    android:padding="8dp">

                    <ImageView
                        android:id="@+id/img_antarsendiri"
                        android:layout_width="90dp"
                        android:layout_height="90dp"
                        android:padding="4dp"
                        android:src="@drawable/g4w_antarsendiri" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="Antar Sendiri"
                        android:textColor="#000000"
                        android:textSize="12dp" />
                </LinearLayout>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:id="@+id/jemput_sekarang"
                android:layout_width="105dp"
                android:layout_height="135dp"
                android:layout_margin="5dp"
                tools:layout_editor_absoluteX="51dp"
                tools:layout_editor_absoluteY="51dp">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:orientation="vertical"
                    android:padding="8dp">

                    <ImageView
                        android:layout_width="90dp"
                        android:layout_height="90dp"
                        android:padding="4dp"
                        android:src="@drawable/g4w_jemputsekarang" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="Jemput Sekarang"
                        android:textColor="#000000"
                        android:textSize="11dp" />
                </LinearLayout>
            </android.support.v7.widget.CardView>
        </LinearLayout>


        <TextView
            android:id="@+id/recyler_title"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Harga Per Kg"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/layoutmainfeature" />


        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:nestedScrollingEnabled="true"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/recyler_title" />


    </android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>

1 个答案:

答案 0 :(得分:0)

从回收者视图中删除这两行,然后重试

app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent

这使您的回收站视图像这样

  <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:nestedScrollingEnabled="true"
        app:layout_constraintTop_toBottomOf="@+id/recyler_title" />