Reputation: 930
I have a RecycleView
where i add items vertically:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:gravity="top">
<android.support.v7.widget.RecyclerView
android:id="@+id/contrattiRecycle"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:layout_weight="1"/>
</LinearLayout>
and the itemcontainer is like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/ore"
android:layout_weight="1"
android:foregroundGravity="center"
android:gravity="center_horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/contratti"
android:layout_weight="1"
android:gravity="center_horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/resa"
android:layout_weight="1"
android:gravity="center_horizontal"/>
</LinearLayout>
When i fill the RecycleView
with items they won't align properly but appear like the picture below:
The RecycleView
is filled in a normal way. As long as every layout width is: android:layout_width="match_parent"
it should expand this layouts to the parents width and the items logically should be aligned. Any suggestion?
Upvotes: 0
Views: 75
Reputation: 15001
android:layout_weight="1"
as you use the layout_weight
property,if you set android:layout_width="wrap_content"
,The system first assigns the three textviews their width wrap_content (wide enough to contain their content), and then assigns the remaining screen space to the three textviews according to the ratio of 1:1:1,so your content is not the same width, it will not appear aligned.
you just need to change TextView
android:layout_width="wrap_content"
to
android:layout_width="0dp"
in this case,it will divide the width by 1:1:1 first
Upvotes: 2
Reputation: 872
For both the linear layouts and text views make:
android:layout_gravity="center"
android:gravity="center"
not center_vertical
or center_horizontal
Upvotes: 1