转自:http://63230860.blog.163.com/blog/static/65880996201321133014272/
其中说几个属性,在tablelayout 中,android:stretchColumns="*" 这一个是设置自动拉伸列的,把各个列拉伸以满足屏幕
在tableRow中的view里面,对每一个view的android:layout_width="1dip",这样设置可以使各个列的宽度保持一致
代码:为了简洁一点,就少贴出来3列
xml
<TableLayout
android:id="@+id/work_censustable_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dip"
android:stretchColumns="*"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="1dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/work_census_comm"
android:background="@drawable/table_view"
android:textSize="11dp" />
<TextView
android:layout_width="1dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/work_inspect"
android:background="@drawable/table_view"
android:textSize="11dp" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="1dip"
android:background="@drawable/table_view"
android:gravity="center"
android:text="合计"
android:textSize="11dp" />
<TextView
android:layout_width="1dip"
android:background="@drawable/table_view"
android:gravity="center"
android:text="1"
android:textSize="11dp" />
</TableRow>
</TableLayout>
Activity:
TableLayout layout = (TableLayout) findViewById(R.id.work_censustable_id);
TableRow row = new TableRow(this);
TextView textView1 = new TextView(this);
textView1.setText("社区"+k);
textView1.setTextSize(11);
textView1.setBackgroundResource(R.drawable.table_view);
textView1.setGravity(Gravity.CENTER);
row.addView(textView1);
TextView textView2 = new TextView(this);
textView2.setText("1");
textView2.setTextSize(11);
textView2.setBackgroundResource(R.drawable.table_view);
textView2.setGravity(Gravity.CENTER);
row.addView(textView2);
TextView textView3 = new TextView(this);
textView3.setText("6");
textView3.setTextSize(11);
textView3.setBackgroundResource(R.drawable.table_view);
textView3.setGravity(Gravity.CENTER);
row.addView(textView3);
layout.addView(row,new TableLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));