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.
But I didn't succeed.
This is my code.
This is xml file which contains my recyclerview.
<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
<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 :
<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
<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.
In the parent of the element 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
<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!
:)
Please update version of a library in gradle file OR to further :
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.