当RecyclerView父对象为ConstraintLayout时,RecyclerView卡项目不会居中?

时间:2018-11-11 20:42:40

标签: android android-recyclerview

我有一张卡片视图列表,其中包含国家的国旗和名称。 CardView具有一个ConstraintLayout作为用于对齐内部视图的子级,并具有LinearLayout作为父级。

MainActivity中的RecyclerView具有一个ConstraintLayout作为Parent(这是AndroidStudio在创建新项目时默认创建的父视图)

<?xml version="1.0" encoding="utf-8"?> <!-- MAIN ACTIVITY XML-->
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_marginTop="10dp"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     android:orientation="vertical">
<android.support.v7.widget.CardView
     android:layout_width="@dimen/card_view_item_main_width"
     android:layout_height="@dimen/card_view_item_main_height">

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

        <ImageView
                android:id="@+id/countryCardImage"
                android:layout_width="match_parent"
                android:layout_height="128dp"
                android:contentDescription="@string/imagen_pais"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"/>

        <TextView
                android:id="@+id/countryCardText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="visible"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintVertical_bias="1"
                />

        </android.support.constraint.ConstraintLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>



<?xml version="1.0" encoding="utf-8"?> <!-- ITEMMAIN.XML -->
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".main.ui.main.MainActivity">

<android.support.v7.widget.RecyclerView
     android:id="@+id/listMain"
     android:layout_width="0dp"
     android:layout_height="0dp"
     android:scrollbars="vertical"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintTop_toTopOf="parent"
     app:layout_constraintBottom_toBottomOf="parent"
     app:layout_constraintLeft_toLeftOf="parent"
     app:layout_constraintRight_toRightOf="parent"
     app:layout_constraintStart_toStartOf="parent"
     app:layout_constraintHorizontal_weight="0"
    />
</android.support.constraint.ConstraintLayout>

Result

2 个答案:

答案 0 :(得分:0)

在项目根视图中,layout_gravity="center"LinearLayout上无法与layout_width="wrap_content:一起在RecyclerView上工作,因此您可以尝试将其宽度更改为{{ 1}},然后将重力更改为match_parent或将android:gravity="center"移至其子视图:

android:layout_gravity="center"

答案 1 :(得分:0)

问题出在布局项目中,您在LinearLayout中设置layout_width =“ wrap_content”,在主布局中,Recyclerview中的layout_width为0dp(必须为match_parent),因此它不在主布局中居中,因为主布局取决于项目布局=>在这种情况下是错误的。 在您的布局项中,更改如下:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">