我有一個自定義XML文件。我想在一個佈局(比如說Relative)n次重複這個動作(顯然)。多次在佈局中添加自定義視圖
我看過很多帖子,但都沒有幫助。我不是在尋找ListView
或Adapters
左右。它就像 - A RelativeLayout
一樣簡單。在它內部,將自定義XML添加到另一個之上。任何次數。
使用靜態LinearLayout
(垂直方向),動態添加視圖會導致呈現一次,而不是一個接一個。不知道爲什麼。雖然TextView
左右在LinearLayout
(垂直)內的循環中重複一個。
然後我動態地創建了佈局(相對),並誇大了自定義XML。顯示一個。當我第一次嘗試另一個時,它告訴我先移除孩子的父母(例外)。如果我這樣做並再次添加,就像移除第一個渲染視圖並再次添加它一樣。
那麼如何在同一個佈局中獲得多個視圖?
什麼,我已經嘗試粗略的介紹:
mainLayout = (RelativeLayout)findViewById(R.id.mainlay); //Mainlayout containing some views already
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW,R.id.sideLayout); //sideLayout is an existing LinearLayout within the main layout.
View child = getLayoutInflater().inflate(R.layout.dynamiccustomlayout,null);
RelativeLayout r1 = new RelativeLayout(this);
r1.setLayoutParams(params);
r1.addView(child);
mainLayout.addView(r1);
mainLayout.setLayoutParams(params);
mainLayout.addView(child);
/* r2 = new RelativeLayout(this);
r2.setLayoutParams(params);
r2.addView(contentLayout); [Gives exception] */
@ 2red13:你的回答可能是對的,但我解釋了我的情況是如何失敗的。你的樣本模擬代碼對於像我這樣的初學者來說有點保險,所以我對此無法判斷,但是我肯定可以說 - 非常感謝你的幫助。 :) – Shridhar