10

guys. I'm trying to code a layout for an activity which contains 4 different fragments, a list fragment and three detail-related fragments. I try to make it look like the following diagram enter image description here

With LinearLayout I can get the 30/70 ratio between the list fragment and the detail area (where the other three fragments are supposed to be). However, when dealing with these three fragments I really don't know how to keep them within the ratios I expect them to have, since you cannot nest layouts with weightSum attributes.

I've been trying with RelativeLayouts but don't want to go with 'wrap_content' values, since some of the contents be bigger than others, breaking thus the appearance I'm trying to achieve.

Is there a way to do this? I know of the TableLayouts, but AFAIK they're like HTML tables: something to use with data, not as a lay out tool...

Frank
  • 2,777
  • 5
  • 18
  • 30
  • What's exactly the problem with nested LinearLayout ? Because it you really want to define some 30/70 or 50/50 ratios, they're your best (and maybe only) move. – Orabîg Jan 28 '13 at 15:50
  • Yes, they're great for, let's say, the bigger part. If I had 2 fragments (list fragment and a Detail fragment that included the 3 others) that would be my choice. However, since these 3 fragments must go in 2 columns, they would need an additional LinearLayout with weightSum to make those 2 columns. And the column on the right would need yet another LinearLayout with weightSum. I've already tried to nest similar stuff and android didn't let me :( – Frank Jan 28 '13 at 15:57

2 Answers2

13

Nested weights should work just fine, I've used them a few times although eclipse shows a hint telling that "nested weights are bad for performance".

You should try something like:

<LinearLayout android:id="@+id/main_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:weightSum="1"
    android:orientation="horizontal">

    <LinearLayout android:id="@+id/layout_fragment_a"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="0.5"/>

    <LinearLayout android:id="@+id/layout_container_b_c"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:weightSum="1"
        android:orientation="vertical">

        <LinearLayout android:id="@+id/layout_fragment_b"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="0.7"/>

        <LinearLayout android:id="@+id/layout_fragment_c"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="0.3"/>

    </LinearLayout>

</LinearLayout>

And that's the way I've done it other times. The xml can have some failures and typos (writting rigth now here in the response box :P) but it should help you getting the idea: one main layout (main_layout) using full space and containing two second level layouts 50% width each one (fragment_a and container_b_C) and another tow layouts in onw of the second level layouts splitting the space in that layout 70/30 (fragment_b and fragment_c) :)

Hope it helps!

petrnohejl
  • 7,581
  • 3
  • 51
  • 63
luixal
  • 906
  • 1
  • 9
  • 10
  • That was it, "Bad for performance". I knew there was something 'wrong' about it, but I am told (both by you and by people outside SO) that it is _the_ way to go. Thanks for the confirmation :D – Frank Jan 28 '13 at 18:21
  • thanks for showing the strategic use of "match_parent". using "wrap_content" doesn't work so well with nested weighed layouts – Someone Somewhere Oct 17 '14 at 21:00
  • Well eclipse never said that it wont work. If you use it for a list, the scroll will be slow because everytime the rendering happens the view has to be measured. – Sreekanth Karumanaghat Sep 22 '17 at 11:53
0

Why not trying :

<LinearLayout android_width="30.0" android_height="fill_parent"/>
<LinearLayout android_width="50.0" android_height="fill_parent"/>
<LinearLayout android_width="50.0" android_height="fill_parent">
      <LinearLayout android_width="fill_parent" android_height="70.0"/>
      <LinearLayout android_width="fill_parent" android_height="30.0"/>
</LinearLayout>

I didn't test it (not on my Eclipse worspace right now), but this should do the trick...

Orabîg
  • 11,718
  • 6
  • 38
  • 58
  • Well, I've just tried and Eclipse says Dimension "70.0" in attribute "layout_height" is missing unit! – Frank Jan 28 '13 at 16:12
  • Yep, sorry. I was meaning 0.7 instead of 70.0 (didn't used these parameters till a long time...). However, you had your answer in the meantime. – Orabîg Jan 28 '13 at 20:24