Android 在RecyclerView中居中自定义视图

Android 在RecyclerView中居中自定义视图,android,android-layout,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"

我有一个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>
然而,似乎没有任何效果

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


我假设你说的是卡片项目没有居中。如果是这样,我建议使用TableLayout并设置stretchColumn=0。这使得卡片项目位于“回收者”视图的中心和唯一列中。 因此,在您的情况下,只需使用TableLayout和TableRow包装您的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>

至少在支持库上,卡视图布局嵌入在
框架布局中,而这一个是您出现问题的罪魁祸首

由于您不能以干净的方式处理它,因此我建议将您的
CardView
嵌入另一个布局中,在该布局中,您有
width=match\u 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>

...

您似乎有卡剪辑问题。看看他们在哪里有一条直线切割卡片下面的阴影。。。添加

android:clipToPadding=“false”
android:clipChildren=“false”

在您的布局中:)

不幸的是,这对我不起作用。我的卡片项目仍然在布局的左侧对齐。我尝试了这个方法,但问题是线性布局的边缘是可见的,这是什么意思?布局没有任何可见的零件/边,除非您自己进行设置。你能分享一张截图吗为了让我明白。好的,我添加了一个截图。你可以看到卡片阴影下的边缘。哦,我看到了,边缘不是很明显,但是卡片视图不适合线性布局,因为它的阴影。卡片和布局之间没有上下边距(xml格式),我假设decorator中计算的边距对于卡片上设置的标高来说太小了。还请记住,在棒棒糖+,阴影会扩大卡的大小,而在棒棒糖-阴影会缩小设备的大小。您可以将卡片视图上的边距从0dp更改为其他内容,然后查看卡片上的标高有何好处。哦,我明白了,因此基本上,修复是在更改布局边距。。谢谢你的回复
<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>
<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>