在RecyclerView中居中自定义视图

时间:2015-08-05 20:03:35

标签: android android-layout

我有一个cardview项目:

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="330dp"
    android:gravity="center"
    android:layout_height="190dp"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="10dp"
    android:layout_marginTop="0dp"
    android:layout_marginBottom="0dp"
    android:foregroundGravity="center_horizontal">
    <TextView
        android:id="@+id/grade"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|center_horizontal"
        android:textSize="30dp" />
</android.support.v7.widget.CardView>

包含RecyclerView的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@style/AppTheme"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal">
    <include
        layout="@layout/toolbar"
        android:id="@+id/tb">
    </include>
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:gravity="center">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/grade_list"
            android:scrollbars="none"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#263238"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal" />
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

我有一个适配器,用cardview项填充RecyclerView。但是,他们没有集中精力。相反,它们似乎与我视图的左侧对齐。我尝试了几种解决方案,包括在我的RecyclerView活动的OnCreate方法中使用以下代码:

gradeList = (RecyclerView)findViewById(R.id.grade_list);
    LinearLayoutManager lm = new    LinearLayoutManager(getApplicationContext());
    lm.setOrientation(lm.VERTICAL);
    gradeList.setLayoutManager(lm);
    gradeList.setAdapter(new RecyclerAdapter(gradeData));
    gradeList.addItemDecoration(new RecyclerView.ItemDecoration() {

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int totalWidth = parent.getWidth();
            int maxCardWidth = getResources().getDimensionPixelOffset(R.dimen.card_width);
            int sidePadding = (totalWidth - maxCardWidth) / 2;
            sidePadding = Math.max(0, sidePadding);
            outRect.set(0,sidePadding,0,sidePadding);
        }
    });

然而,似乎没有任何工作。

编辑:当使用布局作为我的卡片项目的根目录时:

enter image description here

3 个答案:

答案 0 :(得分:1)

我假设您正在谈论卡片物品没有居中。如果是这样,我建议使用TableLayout并设置stretchColumn = 0。这使得卡片项目位于Recycler View的中心和唯一列。 因此,在您的情况下,只需使用TableLayout和TableRow包装您的Recycler View。以下是我在当前的Recycler View Layout中使用的代码示例:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="your_context"
    android:id="@+id/dataStructuresView"
    android:background="@drawable/background">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0">
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp">
            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/recyclerView">
            </android.support.v7.widget.RecyclerView>
        </TableRow>

    </TableLayout>

</FrameLayout>

答案 1 :(得分:1)

至少在支持库上,卡片视图布局嵌入在FrameLayout中,而这是您问题的罪魁祸首。

由于您无法以干净的方式处理它,我建议您将CardView嵌入到另一个布局中,其中width=match_parent并且其内容居中。我尝试了LinearLayout

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">
    <android.support.v7.widget.CardView>
        ...
    </android.support.v7.widget.CardView>
</LinearLayout>

答案 2 :(得分:0)

你似乎有卡片剪裁的问题。看看他们有一条直线切割卡片下面的阴影...添加 android:clipToPadding="false" android:clipChildren="false" 在你的布局:)