简体   繁体   English

如何将水平回收器视图居中?

[英]How to center horizontal recyclerview in center?

How can I make horizontal recyclerview in the center?如何在中心制作水平回收视图?

I have tried many ways putting android:layout_gravity="center" in recyclerView or putting recyclerview in linear layout.我尝试了很多方法将android:layout_gravity="center"放在 recyclerView 中或将 recyclerview 放在线性布局中。

But I didn't succeed.但我没有成功。

This is my code.这是我的代码。

This is xml file which contains my recyclerview.这是包含我的 recyclerview 的 xml 文件。

    <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="horizontal">
   
       <android.support.v7.widget.RecyclerView
           android:layout_width="wrap_content"
           android:layout_height="40dp"
           android:visibility="invisible"
           android:layout_gravity="center_horizontal"
           android:layout_marginTop="-20dp"
           android:id="@+id/recycler_view">
       </android.support.v7.widget.RecyclerView>
   
   </LinearLayout>

item_row.xml file item_row.xml 文件

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="30dp"
            android:layout_height="match_parent"
            android:id="@+id/textview_size"
            android:background="@drawable/textview_border"
            android:gravity="center"
            android:textColor="#000000"
            />
    
    </LinearLayout>

You have to set the Recyleview's layout width to : wrap_content This one should work :您必须将 Recyleview 的布局宽度设置为: wrap_content 这个应该可以工作:

         <android.support.v7.widget.RecyclerView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                >

只需将android:gravity="center"到包含您的RecyclerView LinearLayout

use RelativeLayout insted of LinearLayout than use android:layout_centerInParent="true" of your RelativeLayout like this使用LinearLayout RelativeLayout插入,而不是使用android:layout_centerInParent="true"你的RelativeLayout像这样

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal">

    <android.support.v7.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:visibility="invisible"
        android:layout_centerInParent="true"
        android:layout_marginTop="-20dp"
        android:id="@+id/recycler_view">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

3 years later but I hope my answer helps another dev. 3 年后,但我希望我的回答能帮助另一个开发人员。

In the parent of the element xml:在元素 xml 的父级中:

    android:layout_width="match_parent"

then set your child views to be a specific width然后将您的子视图设置为特定宽度

        android:layout_width="300dp"

And don't forget to add:并且不要忘记添加:

app:layout_constraintLeft_toLeftOf="parent" //If you're using constraint layout as parentw
app:layout_constraintRight_toRightOf="parent"

Use android:layout_gravity="center_horizontal" in your linear layout在线性布局中使用 android:layout_gravity="center_horizo​​ntal"

<LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:layout_gravity="center_horizontal">

            <android.support.v7.widget.RecyclerView
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:visibility="invisible"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="-20dp"
                android:id="@+id/recycler_view">
            </android.support.v7.widget.RecyclerView>

    </LinearLayout>

Try this试试这个

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_gravity="center_vertical|center_horizontal"
    android:gravity="center">

     <android.support.v7.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:visibility="invisible"
        android:layout_gravity="center_horizontal"
        android:id="@+id/recycler_view">
      </android.support.v7.widget.RecyclerView>

</LinearLayout>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.v7.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:visibility="visible"
        android:id="@+id/recycler_view">
    </android.support.v7.widget.RecyclerView>

</LinearLayout>

Just updated recyclerview version in gradle file and it worked now!刚刚更新了 gradle 文件中的 recyclerview 版本,现在可以使用了!

:) :)

Please update version of a library in gradle file OR to further :请更新 gradle 文件中的库版本或进一步:

compile 'com.android.support:recyclerview-v7:23.2.1'

For me, adding对我来说,添加

android: layout_width = "match_parent" 

and

android: layout_weight = "1" 

to the recyclerview item xml worked.到 recyclerview 项目 xml 工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM