1

when I learn fragment on

http://developer.android.com/reference/android/app/Fragment.html

I saw the following code in layout-land/fragment_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
            android:id="@+id/titles" android:layout_weight="1"
            android:layout_width="0px" android:layout_height="match_parent" />

    <FrameLayout android:id="@+id/details" android:layout_weight="1"
            android:layout_width="0px" android:layout_height="match_parent"
            android:background="?android:attr/detailsElementBackground" />

</LinearLayout>

why the layout_width's of fragment and framelayout are both set to zero ?? I tried the same xml on portrait orientation, setting android:orientation="vertical", and a blank screen was shown. So what's the secret in there? I mean what's the difference between vertical and horizontal? what's the special feature of that FrameLayout ?

Flybywind
  • 968
  • 1
  • 11
  • 23
  • 2
    the secret is ***android:layout_weight="1"*** [learn more](http://stackoverflow.com/a/3996104/488434) – Ahmad Kayyali Feb 24 '13 at 08:02
  • Its a fix to a bug on some versions of android where layout weight isn't respected correctly if the width is wrap_content or fill_parent. For some reason that makes it correctly weigh the two equally across full-screen. – Gabe Sechan Feb 24 '13 at 08:02
  • @AhmadAl-Kayyali Thank you very much :) I change the xml as `android:layout_width="match_parent" android:layout_height="0px"` when vertical, It worked! – Flybywind Feb 24 '13 at 08:13

2 Answers2

1

the thing you are looking for is the weight property which is dividing your LinearLayout into two equally portions.

The secret is android:layout_weight="1" learn more

Ahmad Kayyali
  • 8,233
  • 13
  • 49
  • 83
0

If your LinearLayout is vertical orientation than each item with layout_weight attribute have to set layout_height to 0

In case of horizontal LinearLayout you have to set layout_width to 0 if it has layout_weight

Nantaphop
  • 570
  • 2
  • 13