RecyclerView重力

ore*_*ion 9 android android-recyclerview

我有一个recylcler视图,但似乎没有实现setGravity方法.

我的列表是水平对齐的,但我需要它居中.我怎么做?

fragment_my_zone_item_controlled.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dark_grey">

    <FrameLayout
        android:id="@+id/fragment_my_zone_item_controlled_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/fragment_my_zone_item_controlled_tabs"
        android:layout_marginBottom="12dp"
        android:layout_marginTop="12dp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_my_zone_item_controlled_tabs"
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:background="@color/action_bar_color"
        android:scrollbars="none" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是它的外观

这就是它的外观,我需要它的中心

感谢你们

Emi*_*enT 7

我知道这是非常晚的答案,可能是其中一些人得到了这方面的帮助所以请尝试添加这样的

 <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_my_zone_item_controlled_tabs"
        android:layout_width="wrap_content"
        android:layout_height="44dp"
        android:layout_centerHorizontal="true"
        android:background="#dddddd"
        android:scrollbars="none" />
Run Code Online (Sandbox Code Playgroud)

添加 android:layout_centerHorizontal="true"方法

  • 另一个重要的变化是将`android:layout_width`设置为`wrap_content`. (3认同)
  • RecyclerView 没有 `layout_center_horizo​​n` 这样的属性 (3认同)
  • 这会将一个 `RecyclerView` 居中放置在其父级 - `RelativeLayout` 中。但这不会影响 Recycler 的物品或当 Recycler 的宽度为 `match_parent` 时。 (2认同)