2012-02-16 135 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