簡體   English   中英

如何使用布局XML文件中定義的視圖作為模板來以編程方式創建視圖

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

我想通過編程方式填充在布局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:將該行拉出到單獨的布局文件中

第2步:每當需要一行時就對該布局文件進行充氣

步驟#3:在TableLayout上調用addView()以添加膨脹行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM