简体   繁体   中英

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. I have define Table with a single row defining its header, with all the attributes set. 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.

Here is the XML code for the layout file defining the table

<?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

Step #2: Inflate that layout file whenever you need a row

Step #3: Call addView() on the TableLayout to add the inflated row

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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