我已經看到這個問題添加到棧上,但信息還沒有幫助或成功,所以我仍然不確定。動態添加新的窗口小部件控件到現有的佈局
我想要做的要點:
我有一個在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我沒有看到新的項目,通過加強和看會添加控件使用上面的代碼後添加到我的佈局。
不確定它是什麼,但除了它不工作的事實之外,這種方法似乎並不正確。當活動加載時,所有靜態視圖都會設置並在視圖中顯示。我彈出一個加載對話框,通過異步任務的東西,我想期待看到動態控件逐一添加到佈局?
我想有一種方法,我可以膨脹一個新的佈局在xml中定義每個新的DisplayItem,我想添加到我現有的佈局....像這樣,http://stackoverflow.com/questions/7977071/how-to-dynamically-add-a-layout – topwik