TableLayout中一种特别的代码加载实现
今天看见了一个人的问题 他说 他想 在一个静态的tabLayout中也就是在xml中,加入几个TableRow
每个tableRow 有几个别的控件,而这个控件 最好在xml中 因为这样省去了啰嗦的代码。
?
我想了好久没有实现,还是一个牛人给出了解法? 如下:
?
TableLayout table = (TableLayout)findViewById(R.id.table);?
?
LayoutInflater inflater = getLayoutInflater();?
?
for(int i = 0; i < 10; i++) {?
? ? TableRow row = (TableRow)inflater.inflate(R.id.table_row, ?
? ? ? ? table, false);?
?
? ? TextView text = (TextView)row.findViewById(R.id.text);?
? ? text.setText("row: " + i);?
? ? // other customizations to the row?
?
? ? table.addView(row);?
}?
table_row.xml
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">?
? ? <TextView android:id="@+id/text" />?
? ? <TextView android:id="@+id/text2" />?
</TableRow>
这里解释一下TableLayout table = (TableLayout)findViewById(R.id.table);?是在另一个xml当中.关键就是
?TableRow row = (TableRow)inflater.inflate(R.id.table_row, ?
? ? ? ? table, false);?
第二个参数 table是作为根目录的
?