2012-02-16 54 views
0

我有一個垂直LinearLayoutandroid:layout_height = "fill_parent"垂直線性佈局按比例分配兒童視圖

現在,我根據數據庫中的某些參數動態地添加了多個子視圖,有時是2個視圖,其他時間是3個。

我想知道如何以編程方式將這些動態添加的子視圖分佈在垂直LinearLayout中,以便它們均勻分佈。

順便說一句,我無法使用GridView如我在水平ScrollView以嵌入此LinearLayout,以及使用GridView水平ScrollView內是高度灰心。

編輯:添加源和XML文件:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/serverScreensScreen1LL1" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#FFFFFF" > 

<ScrollView 
     android:id="@+id/serverScreensScreen1SV1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fillViewport="true" 
     > 

    <HorizontalScrollView 
     android:id="@+id/serverScreensScreen1HSV1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fillViewport="true" 
     > 

     <LinearLayout 
      android:orientation="horizontal" 
      android:id="@+id/serverScreensScreen1LL2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" > 

     </LinearLayout> 
    </HorizontalScrollView> 
</ScrollView> 
</LinearLayout> <!-- Main Container --> 

來源:

private void drawContent() 
{ 
    LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(fpfp); 

    LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(wcwc); 
    lp3.weight = 0.3F; 

    LinearLayout ll1 = (LinearLayout) findViewById(R.id.serverScreensScreen1LL2); 

    for (int i=0; i<20; i++) 
    { 
      LinearLayout ll2 = new LinearLayout(this); 
      ll2.setOrientation(LinearLayout.VERTICAL); 
      ll2.setLayoutParams(new   LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,  LinearLayout.LayoutParams.FILL_PARENT)); 
      ll2.setWeightSum(1.0F); 

       LinearLayout ll3 = new LinearLayout(this); 
       ll3.setOrientation(LinearLayout.VERTICAL); 
       ll3.setLayoutParams(lp3); 
        TextView tv1 = new TextView(mContext); 
        tv1.setText("Sample Text A: " + i); 
        tv1.setBackgroundColor(Color.rgb(12, 34, 56 + i * 8)); 
        tv1.setLayoutParams(wcwc); 
       ll3.addView(tv1); 


       LinearLayout ll4 = new LinearLayout(this); 
       ll4.setOrientation(LinearLayout.VERTICAL); 
       ll4.setLayoutParams(lp3); 
        TextView tv2 = new TextView(mContext); 
        tv2.setText("Sample Text B: " + i); 
        tv2.setBackgroundColor(Color.rgb(34, 12 + i * 8, 56)); 
        tv2.setLayoutParams(wcwc); 
       ll4.addView(tv2); 

       LinearLayout ll5 = new LinearLayout(this); 
       ll5.setOrientation(LinearLayout.VERTICAL); 
       ll5.setLayoutParams(lp3);    
        TextView tv3 = new TextView(mContext); 
        tv3.setText("Sample Text C: " + i); 
        tv3.setBackgroundColor(Color.rgb(56 + i * 8, 34, 12)); 
        tv3.setLayoutParams(wcwc); 
       ll5.addView(tv3); 

      ll2.addView(ll3); 
      ll2.addView(ll4); 
      ll2.addView(ll5); 

     ll1.addView(ll2); 
    } 
} 

任何建議PLZ ...

+0

,設置機器人:layout_weight =「1」 – Booger 2012-02-16 13:23:01

+0

即力的工作,已經嘗試過:(... – 2012-02-16 13:25:24

+0

尼廷邦薩爾,也嘗試一次layout_weight - 這是你所需要的設定 – Silver 2012-02-16 14:13:33

回答

0

我不知道你是怎麼想的分配意見,但你可以嘗試玩重量。

LinearLayout的文檔中搜索android:weightSum

每個孩子
+0

我更新了我的問題與源文件。請你看看我是否缺少任何東西... thnx提前... – 2012-02-16 13:56:12

+0

@NitinBansal:你可以繪製或創建你想要的圖像實現?有一個'ScrollView'與'Horizo​​ntalScrollView'裏面看起來不太好。 – Macarse 2012-02-16 14:14:30

+0

@Sure ...這是我想要的大桶的繪圖:http://twitpic.com/8kor38 .... btw,我嵌入scrollview內的所有內容,以解決我需要多行的情況,因此,佔用更多的可見區域比可用。如果你可以建議更好的選擇,請讓我知道。即使我不贊成這樣做 – 2012-02-16 14:48:17