layout_weight inconsistency
-
19-06-2021 - |
Question
My app should look like this:
This is how it should look, though the way I had it before it wasn't scaling well. So that's why I am doing this, but it is giving me lots of problems.
But with even seemingly identical setups, the second row takes up all of the room, making it look like this:
Any explanation why it is doing this? It beats me. Here is my XML, any solutions?
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="7.0"
>
<LinearLayout
android:id="@+id/LinearLayout03"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.0" >
<ImageView
android:id="@+id/terranlogo1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:src="@drawable/terranlogo" />
<ImageView
android:id="@+id/protosslogo1"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:layout_height="wrap_content"
android:src="@drawable/protosslogo" />
<ImageView
android:id="@+id/zerglogo1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:src="@drawable/zerglogo" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout04"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.0" >
<CheckBox
android:id="@+id/ck_t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"/>
<CheckBox
android:id="@+id/ck_p1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_weight="1.0" />
<CheckBox
android:id="@+id/ck_z1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_weight="1.0" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout05"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" >
<ImageView
android:id="@+id/terranlogo2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:src="@drawable/terranlogo" />
<ImageView
android:id="@+id/protosslogo2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:src="@drawable/protosslogo" />
<ImageView
android:id="@+id/zerglogo2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:src="@drawable/zerglogo" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout06"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1.0" >
<CheckBox
android:id="@+id/ck_t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
<CheckBox
android:id="@+id/ck_p2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:layout_marginLeft="25dp" />
<CheckBox
android:id="@+id/ck_z2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:layout_marginLeft="25dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout07"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1.0" >
<TextView
android:id="@+id/textView4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Choose your opponets race(s) and your race(s)" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout08"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1.0" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Game Length"
android:layout_weight="1.0"
android:textSize="15dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Mistakes Allowed"
android:layout_weight="1.0"
android:textSize="15dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer Time"
android:layout_weight="1.0"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout09"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1.0" >
<Spinner
android:id="@+id/s_answertime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
/>
<Spinner
android:id="@+id/s_mistakenumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
/>
<Spinner
android:id="@+id/s_gametime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
/>
</LinearLayout>
</LinearLayout>
Solution
The height on the middle LinearLayout is set to wrap_content, which is not what you want. Set all immediate children of the root LinearLayout to layout_height="0dp" and you will get all items evenly distributed in height.
Also, the weightSum=7 is unneeded if you intend for all elements to take up 100% of the container height.
See http://ugiagonzalez.com/2012/01/19/android-linearlayout-distribution-explained-weight-and-sizes/ for more info.