简体   繁体   English

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

[英]How can i make use of views defined in layout XML file as template to create views programmatic way

I want to populate a table, defined in layout xml file through the programmatic way. 我想通过编程方式填充在布局xml文件中定义的表。 I have define Table with a single row defining its header, with all the attributes set. 我已经定义了Table,其中一行定义了它的标题,并设置了所有属性。 Now i want to know a way so that i can just replicate that header row in the table with new content. 现在我想知道一种方法,这样我就可以用新内容复制表中的标题行。

I tried using inflator inflate(int,view) method, but at runtime it showed up with error. 我尝试使用Inflator inflate(int,view)方法,但在运行时出现错误。

Here is the XML code for the layout file defining the table 这是定义表的布局文件的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>

Step #1: Pull that row out into a separate layout file 步骤1:将该行拉出到单独的布局文件中

Step #2: Inflate that layout file whenever you need a row 第2步:每当需要一行时就对该布局文件进行充气

Step #3: Call addView() on the TableLayout to add the inflated row 步骤#3:在TableLayout上调用addView()以添加膨胀行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM