2012-07-10 101 views
1

我創建mainLayout具有兩個按鈕如何在另一視圖中使用佈局添加inflatter

添加視圖:添加其它佈局

刪除:刪除其他佈局。

<Button 
      android:id="@+id/btnAdd" 
      android:textStyle="bold" 

      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:text="Add View" 
      android:onClick="addView" /> 

     <Button 
      android:id="@+id/btnRemove" 
      android:textStyle="bold" 

      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:text="Remove View" 
      android:onClick="removeView" /> 

現在我寫了下面的代碼添加視圖 當我點擊addView按鈕

   LayoutInflater inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE); 
    view=inflater.inflate(R.layout.other_layout,null); 


    mainLayout.addView(view); 

的視圖如下主要佈局增加。 但我想在addView按鈕下面添加正確的視圖(以上removeView按鈕並且不在主佈局的底部)

我該怎麼做?

回答

1

在兩個按鈕之間添加一個框架佈局。

然後在運行時將您的視圖充滿到framelayout中。

LayoutInflater inflater= (LayoutInflater)this.getSystemService (LAYOUT_INFLATER_SERVICE);  view=inflater.inflate(R.layout.other_layout,null); 
    myframeLayout.addView(view); 
+0

它不工作 我添加以下 <的FrameLayout機器人:layout_width = 「match_parent」 \t \t機器人:layout_height = 「WRAP_CONTENT」> – kamal 2012-07-10 09:11:48

0

創建一個新的佈局:

<merge ...> 
    //your layout 
</merge> 

中添加<include>佈局能見度消失(或不可見)的主要佈局。通過處理你想要的事件,你可以簡單地調整你喜歡的方式。

相關問題