2012-03-28 25 views
0

這是我的看法片段如何更改Android中片段的重量大小?

<?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> 

現在我想從Java不是從XML改變片段重量大小的XML的佈局,反正是有這樣做嗎?

回答

2

我應該把碎片放在一個單獨的LinearLayout中並改變它的重量。

像:

<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> 

在Java

LinearLayout layout1 = (LinearLayout) findViewById(R.id.layout1); 
android.widget.LinearLayout.LayoutParams par = layout1.getLayoutParam(); 
par.weight = NEW value; 
+0

我這樣做了,但它沒有顯示任何錯誤,但沒有影響大小 – 2012-03-28 07:43:27

1

請參考this related question。 如果您將每個片段放在FrameLayouts中,然後爲每個這些幀佈局設置權重,則可以使用它。