Подтвердить что ты не робот

Как центрировать элементы RecyclerView по горизонтали с помощью вертикального GridLayoutManager

У меня есть вертикаль RecyclerView с использованием GridLayoutManager. Я хочу, чтобы каждый столбец центрировался, но столбцы начинаются полностью слева. На следующей картинке видно, о чем я говорю. Я использовал некрасивую цветовую схему для иллюстрации столбцов и фона. Зеленый фон для каждого элемента в RecyclerView, красный фон самого RecyclerView:

http://imgur.com/a/J3HtF

Я настраиваю это с помощью:

mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));

Здесь column_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="120dp"
              android:layout_height="180dp"
              android:orientation="vertical"
              android:padding="4dp">

    <ImageView
        android:id="@+id/movie_column_photo"
        android:layout_width="80dp"
        android:layout_height="120dp"/>

    <TextView
        android:id="@+id/movie_column_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

Здесь xml: обзор переработчика:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/company_details_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>
4b9b3361

Ответ 1

Попробуйте, чтобы элемент столбца заполнил ширину столбца при центрировании всего внутри:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="180dp"
              android:orientation="vertical"
              android:padding="4dp">

    <ImageView
        android:id="@+id/movie_column_photo"
        android:layout_width="80dp"
        android:layout_height="120dp"
        android:layout_gravity="center_horizontal"/>

    <TextView
        android:id="@+id/movie_column_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"/>
</LinearLayout>

Ответ 2

Чтобы восстановить @kris larson answer;

Если вы используете ConstraintLayout, достаточно добавить android:layout_gravity="center" или android:layout_gravity="center_horizontal" к родительскому макету в элементе XML (первый родительский элемент в иерархии).

В вашем случае коды будут выглядеть примерно так: similar :

<android.support.constraint.ConstraintLayout
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:orientation="vertical"
        android:padding="4dp"
        android:layout_gravity="center">

        <ImageView

            android:id="@+id/movie_column_photo"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_width="80dp"
            android:layout_height="120dp"/>

        <TextView
            android:id="@+id/movie_column_title"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

</android.support.constraint.ConstraintLayout>

Ответ 3

Оберните ваше представление переработчика внутри ограничения ограничений, примерно так:

<androidx.constraintlayout.widget.ConstraintLayout
 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="match_parent" >

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/book_list"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:spanCount="2" />

Надеюсь, поможет!!