2015-10-19 58 views
3

我有一個自定義XML文件。我想在一個佈局(比如說Relative)n次重複這個動作(顯然)。多次在佈局中添加自定義視圖

我看過很多帖子,但都沒有幫助。我不是在尋找ListViewAdapters左右。它就像 - 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] */ 

回答

3

這是怎麼計算出我...

在此之前,與Android的問題是:

如果添加內LinearLayout(水平),他們將與水平出現新的動態視圖創建實例,添加到視圖中。

然而令人震驚的是,在LinearLayout(垂直方向)的情況下,情況並不相同。因此整個混亂。

解決方案:

RelativeLayout的佈局文件被綁定與可變,有點像這樣:

customLay = (RelativeLayout) mainLay.findViewById(R.id.dynamicCustomLayout); 

然後,Dynamic RelativeLayout創建在其中前者變量被添加/纏繞。

customLayout = new RelativeLayout(this); 
customLayout.addView(customLay); 

每個佈局分配了id:

customLayout.setId(i); 

然後循環運行(2如果對於i條件= 0和I> 0)

對於i> 0(表示所述第二動態佈局,以低於第一添加),LayoutParameter被創建:

params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 

然後對於i> 0,採用t他IDS的動態視圖,它們被添加一個低於其他:

//Following code below used id to place these views below each other to attain list type arrangement of views// 

    // i==0 for first view on top// 
    if (i == 0) { 
         params.addRule(RelativeLayout.BELOW, R.id.sideLayout); 
         customLayout.setLayoutParams(params); 
        } 
    // i>0 for views that will follow the first top// 
else { 
         params.addRule(RelativeLayout.BELOW, i - 1); 
         customLayout.setLayoutParams(params); 
        } 

然後加入到主根佈局,所有這些觀點或卡需要顯示:

includeLayout.addView(customLayout); 

Ofcourse,代碼不只是這個。我寫下了幫助我實現目標的基本要點,並可能在未來幫助其他人。

所以主要的實質是---

  1. 使用Dynamic RelativeLayout,以
  2. 綁定靜態RelativeLayout,並在IDS的基礎
  3. 將ID分配到Dynamic RelativeLayout包裝,並
  4. 請使用RelativeLayoutParameters將以下 ID放在以前的ID下面。
+0

@ 2red13:你的回答可能是對的,但我解釋了我的情況是如何失敗的。你的樣本模擬代碼對於像我這樣的初學者來說有點保險,所以我對此無法判斷,但是我肯定可以說 - 非常感謝你的幫助。 :) – Shridhar

1

你必須通過自身實例化每一個孩子

View child = getLayoutInflater().inflate(R.layout.dynamiccustomlayout,null); 
r1.addView(child); 
View child2 = getLayoutInflater().inflate(R.layout.dynamiccustomlayout,null); 
r1.addView(child2); 

// OK,我做一個模擬的事情在我的應用程序的obne 。這裏是代碼:

public class FlxForm extends LinearLayout { 
    public FlxForm(Context context) { 
     super(context); 
     inflater = LayoutInflater.from(context); 
     inflater.inflate(R.layout.flxform, this); 
     this.setPadding(0, 0, 0, 0); 
     container = (LinearLayout) this.findViewById(R.id.flxform); 
     this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
     //here is my funtion to calculate the items i want to add, its a little bit too complicated, but in the end it works like: 
     for(int i=0;i<10;i++){ 

     View x = inflater.inflate(R.layout.dynamiccustomlayout,null); 
     container.addview(x); 
    } 
    } 
} 

XML的形式

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/flxform" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:focusable="false" 
    android:background="@android:color/transparent" 
    android:orientation="vertical" > 
</LinearLayout>  

然後你就可以實例化一個「表」 OBJEKT並將其添加到滾動型

+0

在代碼中閱讀我的評論。如果我在mainLayout中添加孩子,孩子坐在以前的視圖或重疊他們。那就是問題所在。 – Shridhar

+0

另一個令人驚訝的方面,'r1.setLayoutParam(params)'這次不起作用。只是自定義視圖覆蓋或隱藏mainLayout中的其他現有視圖。 – Shridhar

+0

編輯了答案,希望它有幫助 – 2red13

0

這樣做你將不得不窩您的RelativeLayout在ScrollView中,並手動管理所有滾動,項目添加,內存管理等。

所以添加自定義視圖的n個簡單的解決方法是使用RecyclerViewListViewGridView等有着簡潔的CustomAdapter和自定義視圖。

下面是使用RecyclerView自定義適配器的一個很好的例子:
http://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465

我希望這有助於。

+0

哦!我同意這可能會有幫助,但這是我想避免的第一件事。這就是爲什麼我在問題中提到沒有適配器(實際上也是ScrollView)。但是,如果什麼都沒有,我會找到自己的方式。一位高管建議跳過靜態xml,而不是動態地創建xml。然後使用ID並在LayoutParameters中使用它們放置在彼此之下。 – Shridhar