TableLayout 实现动态新增一行,与之前行宽度一致

来源:互联网 发布:qq游戏大厅mac版下载 编辑:程序博客网 时间:2024/05/22 15:50

转自:http://63230860.blog.163.com/blog/static/65880996201321133014272/


其中说几个属性,在tablelayout 中,android:stretchColumns="*"  这一个是设置自动拉伸列的,把各个列拉伸以满足屏幕

在tableRow中的view里面,对每一个view的android:layout_width="1dip",这样设置可以使各个列的宽度保持一致

TableLayout 实现动态新增一行,与之前行宽度一致 - ╅藍色さ梦幻 - Priest、、
点击统计、新增一行

 

TableLayout 实现动态新增一行,与之前行宽度一致 - ╅藍色さ梦幻 - Priest、、

 

代码:为了简洁一点,就少贴出来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));

0 0
原创粉丝点击