2012-11-23 36 views
-1

我有一個框架佈局,包含一個edittext和按鈕(用於刪除字段),當用戶單擊按鈕時,我想多次添加到相對佈局。多次添加預定義的framelayout到相對佈局

我已經搜查,但我只是無法找到如何以編程方式做到這一點。

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > 

    <EditText 
     android:id="@+id/inputbox" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:textSize="25sp" 
     android:textStyle="bold" 
     android:hint="@string/inputhint" 
     android:ems="10" 
     android:imeOptions="actionNext" 
     android:singleLine="true" /> 

    <Button 
     android:id="@+id/buttonremove"  
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dp" 
     android:layout_gravity="right|center_vertical" 
     android:background="@drawable/remove" /> 

</FrameLayout> 
+0

您希望生成的佈局看起來如何?垂直行中的所有FrameLayouts? – brillenheini

+0

是的。所以會有一排垂直的文字輸入。在使用此框架佈局動態創建的前後有兩個非動態輸入 – QazPhilby

回答

0

當容器是具有垂直方向的LinearLayout而不是RelativeLayout時,容易得多。

ViewGroup container = (ViewGroup) findViewById(R.id.container); 
getLayoutInflater().inflate(R.layout.input, container, true); 

只需檢索LinearLayout(容器),然後使用容器作爲父級對容器進行充氣。將attachToRoot設置爲true時,它將自動添加到容器中。