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

转自: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));

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值