Tuesday, 10 September 2013

Android LinearLayout and weights issue

Android LinearLayout and weights issue

I am having trouble with a LinearLayout and weights in Android. I want a
horizontal LinearLayout to hold 2 vertical LinearLayouts separated by a
single View with a 9 patch background to be the separator between the 2
vertical LinearLayouts. Like this: (outer box is the outer LinearLayout
and the middle double line is my 9 patch separator.)
----------------------------
| one || three |
| two || four |
----------------------------
What keeps happening is the first inner LinearLayout displays with minimal
width to display its content (as if it's width is wrap_content), then the
rest of the space is taken up by the separator view stretched to fill the
rest of the outer LinearLayout. The 2nd inner LinearLayout is not
dipslaying at all.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_weight="1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
</LinearLayout>
<View
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/divider_vertical"
android:layout_weight="0" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_weight="1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
</LinearLayout>
</LinearLayout>
What am I doing wrong here? I cannot figure out for the life of me why the
middle View is taking up all of the space, leaving none for the second
inner LinearLayout...

No comments:

Post a Comment