【问题标题】:Centering custom view inside RecyclerView在 RecyclerView 中居中自定义视图
【发布时间】:2015-08-05 20:03:34
【问题描述】:

我有一个卡片视图项目:

<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>

我有一个适配器,它用卡片视图项填充 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);
        }
    });

但是,似乎没有任何效果。

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

【问题讨论】:

    标签: android android-layout


    【解决方案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>
    

    【讨论】:

    • 我试过了,但问题是线性布局边缘是可见的
    • 你这是什么意思?除非您自己设置,否则布局没有任何可见的部分/边缘。你能分享一个截图吗? - 让我明白。
    • 好吧,我添加了一个截图。您可以在卡片阴影下方看到边缘
    • 哦,我明白了,不是边缘可见,而是 CardView 不适合线性布局,因为它的阴影。卡片和布局(在 xml 中)之间没有顶部或底部边距,我假设在装饰器中计算的边距对于卡片上设置的高程来说太小了。还要记住,在 Lollipop+ 上,阴影会扩大卡片的大小,而在 Lollipop- 上,阴影会缩小设备的大小。您可以将卡片视图上的边距从 0dp 更改为其他值,然后看看什么对卡片上的高度有好处。
    • 哦,我明白了,所以基本上修复是改变布局边距.. 感谢您的回复
    【解决方案2】:

    我假设您说的是卡片项目没有居中。如果是这样,我建议使用 TableLayout 并设置stretchColumn=0。这使得卡片项目位于您的回收站视图的中心且唯一的列中。 因此,在您的情况下,只需使用 TableLayout 和 TableRow 包装您的 Recycler View。这是我在当前 Recycler 视图布局中使用的代码示例:

    <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>
    

    【讨论】:

    • 不幸的是,这对我不起作用。我的卡片项目仍然对齐布局的左侧。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-14
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      相关资源
      最近更新 更多