我如何利用布局XML文件中定义的视图作为模板来创建视图编程方式

时间:2010-02-28 06:26:27

标签: android android-widget

我想填充一个表,通过编程方式在layout xml文件中定义。我已经定义了Table,其中一行定义了它的标题,并设置了所有属性。现在我想知道一种方法,这样我就可以用新内容复制表格中的标题行。

我尝试使用inflator inflate(int,view)方法,但在运行时它出现了错误。

以下是定义表

的布局文件的XML代码
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:stretchColumns="0,1,2"
 android:id="@+id/tLayout"
 > 
 <TableRow
  android:layout_width="fill_parent"
  android:id="@+id/tRow">
  <TextView
   android:id="@+id/name"
   android:padding="3dip"
   android:gravity="left"
   android:text="Name"
   />
  <TextView
   android:id="@+id/address"
   android:padding="3dip"
   android:gravity="left"
   android:text="Address"
   />
  <TextView
   android:id="@+id/age"
   android:padding="3dip"
   android:gravity="left"
   android:text="Age"
   />
 </TableRow>
</TableLayout>

1 个答案:

答案 0 :(得分:1)

步骤1:将该行拉出到单独的布局文件中

步骤2:每当需要一行时,给该布局文件充气

步骤3:致电addView()上的TableLayout以添加充气行