Центр CardView элемент в RecyclerView - PullRequest
0 голосов
/ 06 марта 2019

Я отображаю список продуктов в RecyclerView, где мои

item_row.xml

<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="250dp"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:layout_gravity="center_horizontal"
        android:layout_centerHorizontal="true"
        app:cardUseCompatPadding="true"
        app:cardElevation="4dp"
        app:cardCornerRadius="3dp">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:gravity="center"
            android:orientation="horizontal">

        <TextView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:textAlignment="center"
                android:textStyle="bold"
                android:text="Hello"
                android:textColor="@android:color/background_dark"
                android:textSize="20sp"
                android:id="@+id/tv_subject"/>

        <android.support.v7.widget.AppCompatCheckBox
                android:id="@+id/checkbox"
                android:layout_width="50dp"
                android:layout_height="50dp"/>
    </LinearLayout>

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

Я применил два свойства кCardView, чтобы сделать его в центре, но все же мой элемент View выровнен по левому краю

    android:layout_gravity="center_horizontal"
    android:layout_centerHorizontal="true"

main_layout.xml

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
        <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textSize="24sp"
                android:textColor="@android:color/white"
                android:layout_margin="30dp"
                android:textAlignment="center"
                android:text="Hello World"/>

        <android.support.v4.widget.SwipeRefreshLayout
                android:id="@+id/swipeRefreshLayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

            <android.support.v7.widget.RecyclerView
                    android:id="@+id/recycler_view"
                    android:layout_width="match_parent"
                    android:layout_gravity="center"
                    android:layout_height="match_parent"/>
        </android.support.v4.widget.SwipeRefreshLayout>
    </LinearLayout>

Я также добавил android:layout_gravity="center" в RecyclerView, но все ещеэто пункт Строки выровнены по левому краю ..

Как выровнять CardView (ширина 250 dp) в центре RecyclerView?что я пропускаю.

мой дизайн CardView в окне предварительного просмотра ..

введите описание изображения здесь

Редактировать -> Решение найдено

когда я даю 250dp widhth SwipeRefresh и устанавливаю его layout_gravity="center", тогда он центрирует вид. Но почему другие решения не работают ??

<android.support.v4.widget.SwipeRefreshLayout
                android:id="@+id/swipeRefreshLayout"
                android:layout_width="250dp"
                android:layout_gravity="center"
                android:layout_height="0dp"
                android:layout_weight="1">

            <android.support.v7.widget.RecyclerView
                    android:id="@+id/recycler_view"
                    android:layout_width="match_parent"
                    android:layout_gravity="center"
                    android:layout_height="match_parent"/>
        </android.support.v4.widget.SwipeRefreshLayout>
...