如何将cardview项目集中到recyclerview中

时间:2016-06-05 18:00:38

标签: android android-recyclerview android-cardview

我创建了一个带有recyclerview的collapsingtoolbar,但我无法将cardview对象置于recyclerview中心。正如你所看到的,我在cardview中增加了重力,但它无法正常工作。任何想法????

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
app:expandedTitleMarginStart="60dp">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="255dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".MainActivity"
        android:id="@+id/collapsing_toolbar_layout"
        android:background="@drawable/header1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"

            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin"
            android:transitionGroup="false">

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>

Cardview:

<?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:id="@+id/cardView"
android:layout_width="200dp"
android:layout_height="75dp"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
app:cardCornerRadius="@dimen/activity_vertical_margin"
>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp" />

</RelativeLayout>

这就是我的应用程序的样子: app image

1 个答案:

答案 0 :(得分:1)

将RecyclerView layout_gravity更改为中心