King Wu
King Wu

Reputation: 387

Two fragment can't weight in the Linear layout?

i try to display a two-pane UI design. I try to add two fragments in a linear layout with a weight. However, the linear layout seems to ignore the weight. How do i correct it?? Thanks

[Link:] http://dl.dropbox.com/u/78582670/twopanes.png

my layout:

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

<fragment
    class="com.usci.view.fragment.CatalogFragment"
    android:id="@+id/fragment_catalog"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="0.2">
</fragment>

<fragment
    class="com.usci.education.TestFragment1"
    android:id="@+id/fragment_test"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="0.8">
</fragment>

</LinearLayout>

Solution:

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

<fragment
    class="com.usci.view.fragment.CatalogFragment"
    android:id="@+id/fragment_catalog"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="8">
</fragment>

<fragment
    class="com.usci.education.TestFragment1"
    android:id="@+id/fragment_test"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="2">
</fragment>

</LinearLayout>

Upvotes: 0

Views: 4415

Answers (3)

android developer
android developer

Reputation: 116402

always , when you use weights , set the width/height (of the children) to 0px ,and prefer integers for the weights .

so , the solution is :

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

<fragment
    class="com.usci.view.fragment.CatalogFragment"
    android:id="@+id/fragment_catalog"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight="2">
</fragment>

<fragment
    class="com.usci.education.TestFragment1"
    android:id="@+id/fragment_test"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight="8">
</fragment>

</LinearLayout>

Upvotes: 4

Volodymyr
Volodymyr

Reputation: 1047

Set android:layout_width and android:layout_height value match_parent, then for example set android:layout_weight (1 fragment and 2 fragment) value 1

Upvotes: 0

Nermeen
Nermeen

Reputation: 15973

With me is working by putting 20,80 .. try also 0.8,0.2:

<fragment
class="com.usci.view.fragment.CatalogFragment"
android:id="@+id/fragment_catalog"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.8">
</fragment>

<fragment
    class="com.usci.education.TestFragment1"
    android:id="@+id/fragment_test"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="0.2">
</fragment>

Upvotes: 0

Related Questions