您的位置:首页 > 其它

TableLayout的属性

2016-04-25 09:37 323 查看

自动扩展

有时我们一行可能只有一列,然后下面一行的列数大于一列。那要让特定的列数自动伸展,就得使用

android:strechColumns=”” 表示如果后面有剩下位置,则将各列都伸展,如果* 换成0,则表示第1列伸展,1表示第二列。

TableRow下控件属性:android:layout_span=”n” 表示该控件的占n列。两个结合在一起使用效果最好。

我的表格最多有两列,然后也有一列的。去掉其中一个属性都不能满足要求,无法自动伸展。图1为要求,图二为只去掉strechcolumns,图三为只去掉layout_span的。







动态添加TableRow

代码很简单。先new tablerow,再new需要添加到tablerow的控件,然后往控件添加内容,tableRow.addView(控件名),tableLayout.addView(tableRow)即可。注意的一点就是我一开始是使用同一个TextView添加到TableRow里的。然后一直报错说The specifed child already has a parent。其实就是说该TextView已经被添加到TableRow里了。不能被其他添加。这时如果使用TableRow.removeAllView就不会报错,但是之前添加的TextView没了。所以只能要几列new几个控件。

TableRow tableRow = new TableRow(this);
TextView name = new TextView(this);
TextView credit=new TextView(this);
TextView grade=new TextView(this);
TextView rank=new TextView(this);
TextView gpa=new TextView(this);
name.setSingleLine(false);
name.setWidth(200);
credit.setSingleLine(false);
grade.setSingleLine(false);
rank.setSingleLine(false);
gpa.setSingleLine(false);

tableRow.removeAllViews();

name.setText(g.getName());
tableRow.addView(name);
credit.setText(g.getCredit());
tableRow.addView(credit);
grade.setText(g.getScore());
tableRow.addView(grade);
gpa.setText(g.getGpa());
tableRow.addView(gpa);
rank.setText(g.getRank());
tableRow.addView(rank);
tableLayout.addView(tableRow,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: