我有一个垂直LinearLayout
与android: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 ...
,设置机器人:layout_weight =“1” – Booger 2012-02-16 13:23:01
即力的工作,已经尝试过:(... – 2012-02-16 13:25:24
尼廷邦萨尔,也尝试一次layout_weight - 这是你所需要的设定 – Silver 2012-02-16 14:13:33