2012-08-31 40 views
1

我已經看到這個問題添加到棧上,但信息還沒有幫助或成功,所以我仍然不確定。動態添加新的窗口小部件控件到現有的佈局

我想要做的要點:
我有一個在xml中定義的佈局,例如some_details_view.xml。

setContentView(R.layout.some_details_view); 

它具有利用一個父相對佈局,線性頭佈局,線性頁腳佈局,包含容納一些標籤的相對佈局的中間滾動佈局佈置文本視圖一堆 - 值類型的文本視圖。 並且在滾動視圖相對佈局的底部,我當前放置了一個框架佈局作爲佔位符。

在創建相應的活動時,我在相應的文本視圖中設置了文本,並使用之前活動移交的數據;基本的東西。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/white" > 

    <LinearLayout 
     android:id="@+id/header" 

     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" > 

         ...some header content 

    </LinearLayout>   

    <LinearLayout 
     android:id="@+id/footer" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:orientation="vertical" > 

     ..some footer content 

    </LinearLayout> 

    <ScrollView 
     android:id="@+id/scroll" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@+id/footer" 
     android:layout_below="@+id/header" 
     android:layout_margin="5dip" > 


     <RelativeLayout 
      android:layout_width="fill_parent" 
      android:id="@+id/relativeScroll" 
      android:layout_height="wrap_content" > 

      ...text views in relative layout 

      <FrameLayout 
       android:id="@+id/placeholder" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_below="@+id/moreInfoValue" /> 

     </RelativeLayout> 

    </ScrollView> 

</RelativeLayout> 

設置爲給定的數據文本的意見後,我用一個異步任務中獲得,我想表明作爲靜態形式佈局底部的歷史記錄列表類型的東西一些額外的數據。可能有0個或更多項目,因此我要麼添加1個或更多個文本視圖,要麼根本沒有。

在post執行,我明白在主UI線程上運行,我嘗試找到一個正在退出的容器佈局/視圖組,並添加一個新的線性佈局,我添加新的文本視圖,或者只是添加新文本直接查看現有容器佈局。

這裏是我試過的最新的東西:

ViewGroup mContainer = null; //defined as member variable of activity class and instatiated in on create 
mContainer = (ViewGroup)findViewById(R.id.placeholder); //set in on create 
LinearLayout ll = new LinearLayout(context); //on post execute of async task 
ll.setOrientation(LinearLayout.VERTICAL); 
mContainer.addView(ll); //add a new linear layout to an existing container layout 
//add some new text view widgets items dynamically 
for(NewDisplayItem item : items) 
{ 
    TextView tv = new TextView(context); 
    tv.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); 
    tv.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); 
    tv.setText(item.getSomeText()); 
    ll.addView(tv); //add text view to new linear layout 
} 

當加載UI我沒有看到新的項目,通過加強和看會添加控件使用上面的代碼後添加到我的佈局。

不確定它是什麼,但除了它不工作的事實之外,這種方法似乎並不正確。當活動加載時,所有靜態視圖都會設置並在視圖中顯示。我彈出一個加載對話框,通過異步任務的東西,我想期待看到動態控件逐一添加到佈局?

+0

我想有一種方法,我可以膨脹一個新的佈局在xml中定義每個新的DisplayItem,我想添加到我現有的佈局....像這樣,http://stackoverflow.com/questions/7977071/how-to-dynamically-add-a-layout – topwik

回答

2

首先textView.setWidth(int)以像素爲參數width

其次,您還應該在要添加的LinearLayout上設置佈局參數。

你應該設置LayoutParams的方法如下:

ll.setLayoutParams(new LinearLayout.LayoutParams(
     LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 

同爲YOUT TextViews

+0

哦,雅,正直,謝謝你 – topwik

1

Ovidiu Latcu有一個很好的答案。一個很好的問題是:是否有一個原因,你不使用ListView(btw有什麼情況下,他在做什麼更好)?一個ListView有很多的機制,以幫助您防止內存不足

+0

好吧,我仍然相當綠色,當涉及到Android。我的理解是我不能在滾動視圖中有ListView ....我希望我的表單佈局可以滾動,然後歷史類型列表可以是滾動視圖內的列表視圖,但不允許。或者你說整個相關的文本佈局可能是一個列表視圖...其中文本視圖的內容可以是一個膨脹的行,然後在後執行,我可以膨脹一個不同的佈局,我想要在那裏呈現的項目,只是將它們添加到相同的ListView? – topwik

+0

開始想到它,我寧願爲這些動態內容項目中的每一個充氣特定佈局,而不是試圖在代碼中佈置每個動態項目細節。僅僅設置已經按照我想要的方式佈置的文本視圖的文本會更容易 – topwik

相關問題