0

Here is the xml-Layout of my fragment view

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:weightSum="100"
     >

    <fragment
        android:id="@+id/menuFragment"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="70"

        class="com.pack.Menufragment" >
    </fragment>

    <fragment
        android:id="@+id/bodyFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="30"
        class="com.pack.BodyFragment" >
    </fragment>

</LinearLayout>

Now I want to change the fragment weight size from the java not from XML, is there anyway to do this?

3 Answers 3

2

I should put the Fragments in a seperate LinearLayout and change that weight.

Like:

<LinearLayout 
    android:id="@+id/layout1"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_weight="70"

    <fragment
        android:id="@+id/menuFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        class="com.pack.Menufragment" >
    </fragment>
</LinearLayout>
<LinearLayout 
    android:id="@+id/layout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="30"
    <fragment
        android:id="@+id/bodyFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.pack.BodyFragment" >
    </fragment>
</LinearLayout>

In Java

LinearLayout layout1 = (LinearLayout) findViewById(R.id.layout1);
android.widget.LinearLayout.LayoutParams par = layout1.getLayoutParam();
par.weight = NEW value;
1
  • I did this, but its not showing any error but not taking effect in the size either Mar 28, 2012 at 7:43
1

Please refer to this related question. You can use this if you enclose each of your fragments in FrameLayouts and then set the weights for each of these frame layouts.

0

You might want to have a look at how its done for a RelativeLayout in this thread: Android - How do I specify weight programmatically for a RelativeLayout? Should be similar with layout params for a fragment I assume, haven't tried it so I'm not giving you any guarantees as to if this will work or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.