-2

As describe in the topic's title, I have 3 TextView and I would like them to share the whole screen's width using the android:layout_weight attribute, but doing so didn't get the sought results.

LinearLayout:

<TextView
    android:id="@+id/tour1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/back"
    android:clickable="true"
    android:gravity="center"
    android:paddingBottom="8dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="8dp"
    android:text="Tour \n disponibili  \n 21" />

<TextView
    android:id="@+id/tour2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_toRightOf="@+id/tour1"
    android:layout_weight="1"
    android:background="@drawable/back"
    android:clickable="true"
    android:gravity="center"
    android:paddingBottom="8dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="8dp"
    android:text="Tour \n prenotabili  \n 16" />

<TextView
    android:id="@+id/tour3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_toRightOf="@+id/tour2"
    android:layout_weight="1"
    android:background="@drawable/back"
    android:clickable="true"
    android:gravity="center"
    android:paddingBottom="8dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="8dp"
    android:text="Tour \n preferiti  \n 3" />

result in Portrait:

Portrait

result in Landscape:

Landscape

where is the elephant?

ps. As you can notice from the screenshot, a second problem arises: Why the first 2 TextView get stretched in height? How can I fix this unpredictable behavior?

Anjali Tripathi
  • 1,477
  • 9
  • 28
Cris
  • 2,002
  • 4
  • 30
  • 51

2 Answers2

3

Replace

android:layout_width="wrap_content"

with

android:layout_width="match_parent"

in your LinearLayout.

wrap_content means that the 'ViewGroupwill have the size which is big enough to enclose its content, whilematch_parent` will make it as big as its parent.

aga
  • 27,954
  • 13
  • 86
  • 121
1

The LinearLayout width is set to wrap_content. Set it to match_parent and the views will stretch to the whole screen width.

Lamorak
  • 10,957
  • 9
  • 43
  • 57