在垂直线性布局中按比例分配子视图

时间:2012-02-16 13:19:25

标签: android scrollview android-linearlayout

我有LinearLayout的垂直android:layout_height = "fill_parent"

现在,我根据数据库中的某些参数,动态地为其添加多个子视图,有时是2个视图,其他时间为3。

我想知道如何以编程方式在垂直LinearLayout中分发这些动态添加的子视图,以便它们统一分布。

顺便说一句,我无法使用GridView,因为我必须将此LinearLayout嵌入水平ScrollView,并且在水平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);
    }
}

任何建议都是......

1 个答案:

答案 0 :(得分:0)

我不确定您是如何尝试分发视图的,但您可以尝试使用权重。

LinearLayout的文档中搜索android:weightSum