2014-01-07 140 views
-2

我有一個(複雜)RelativeLayout與幾個TextViews,ImageViews和一個按鈕。該佈局創建的頻率取決於數據庫中的數據。我從列表中獲得這些數據並想要動態顯示它,我該怎麼做?動態地添加xml到佈局

XML動態添加:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/zusatz_content" > 

<TextView 
    android:id="@+id/text1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="20dp" 
    android:layout_marginRight="15dp" 
    android:layout_marginLeft="15dp" 
    android:text="Lorem Ipsum 1" /> 

<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/text2" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="20dp" 
    android:text="@string/button_desc" /> 

<ImageView 
    android:id="@+id/image" 
    android:layout_width="300dp" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/text1" 
    android:layout_centerHorizontal="true" 
    android:contentDescription="@string/ima_zusatz_desc" 
    android:src="@drawable/nix" /> 

<TextView 
    android:id="@+id/text2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/text1" 
    android:layout_below="@+id/image" 
    android:text="Lorem Ipsum 2" /> 

通知:文本和圖像應動態地從數據庫中的內容進行更改。

感謝,TomTom公司

+0

不清楚你在問什麼。 –

+0

如何以編程方式將這些xml佈局添加到父佈局 –

回答

0

除了你的XML,您將需要一個適配器和某種類型的ListView控件。在適配器中,你將不得不實施

public View getView(int position, View convertView, ViewGroup parent) 

在這種方法中,返回上的每個按鈕/文本視圖中顯示的數據新的視圖(LayoutInflator和你的XML可能創建)和更新。

請務必研究convertView的工作原理。如果你的列表很長,使用convertView的性能提升很大。如果你得到了正確的結果,Android的listview和baseadpater將會爲你付出艱辛的努力。