2016-11-23 170 views
0

這是我的佈局,我嵌套在另一個嵌套在滾動視圖內的線性佈局內的線性佈局。現在我要動態地添加一個線性佈局(我甚至可能會增加高達10)內android:id="@+id/formLayout"即beneth android:id="@+id/secondLayout"需要動態添加布局

原始佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="420dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true"> 
     <!-- LinearLayout Inside ScrollView --> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/formLayout" 
      android:orientation="vertical"> 
      <!-- Serial Layout --> 
      <LinearLayout 
       android:id="@+id/secondLayout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="20dp" 
       android:orientation="horizontal" 
       android:weightSum="2"> 
       <android.support.design.widget.TextInputLayout 
        android:id="@+id/serialno_label" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="10dp" 
        android:layout_marginStart="10dp" 
        android:paddingEnd="10dp" 
        android:paddingLeft="10dp" 
        android:paddingStart="10dp"> 
        <AutoCompleteTextView 
         android:id="@+id/fieldSerial" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:hint="@string/txt_sno_text" 
         android:singleLine="true" 
         android:textIsSelectable="false" 
         android:textSize="15sp" /> 
       </android.support.design.widget.TextInputLayout> 
      </LinearLayout> 
     </LinearLayout> 
    </ScrollView> 
</RelativeLayout> 

動態佈局需要添加:

<LinearLayout 
    android:id="@+id/sixthLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="20dp" 
    android:orientation="horizontal" 
android:weightSum="2"> 

<android.support.design.widget.TextInputLayout 
    android:id="@+id/attr2_label" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dp" 
    android:layout_marginStart="10dp" 
    android:paddingEnd="10dp" 
    android:paddingLeft="10dp" 
    android:paddingStart="10dp"> 

    <EditText 
     android:id="@+id/fieldAttr2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:hint="Attribute 2" 
     android:textSize="15sp" /> 
</android.support.design.widget.TextInputLayout> 

</LinearLayout> 

有人可以幫我弄這個嗎 ?

回答

1

使用LayoutInflater根據您的佈局模板創建視圖,然後將其注入到您需要的視圖中。

public static NavigableMap<Integer, String> navigableMap = new TreeMap<Integer, String>(); 

    public static int count = 0; // to count no of views added 
    public static void add_new(final Activity activity) 
     { 
      final LinearLayout linearLayoutForm = (LinearLayout) activity.findViewById(R.id.formLayout); 
      final LinearLayout newView = (LinearLayout) activity.getLayoutInflater().inflate(R.layout.view_to_add, null); 
      newView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
      final EditText edit_new = (EditText) newView.findViewById(R.id.fieldAttr2); 

      edit_new.setId(id++); // It will assign a different id to edittext each time while adding new view. 
      Log.i("actv id",edit_new.getId()+""); // You can check the id assigned to it here. 

      // use a Hashmap or navigable map (helpful if want to navigate through map) 
      // to store the values inserted in edittext along with its ids. 

      edit_new.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(View view, boolean b) { 
       navigableMap.put(edit_new.getId(), edit_new.getText().toString()); 
       Log.i("navigablemap", navigableMap.toString()); 
       } 
      }); 

      // you can get values directly through map by referencing its ids. 

      // OR 

      EditText actv_uc = (EditText) linearLayoutForm.findViewById(navigableMap.lastKey()); 
      // Provide the id of edittext you want to access 
      actv_uc.getText(); // to get value of EditText 
      actv_uc.setText(""); 
      linearLayoutForm.addView(newView); 
      count++; 
     } 

無論何時想要添加新視圖,請調用此函數。如果你在Activity中調用它,那麼就不需要傳遞Activity對象作爲參數。但是如果你在片段中使用它,那麼需要傳遞一個Parent Activity對象來運行。

很容易的和有益的教程動態添加視圖: - http://android-er.blogspot.in/2013/05/add-and-remove-view-dynamically.html

希望這會幫助你。謝謝

+0

謝謝!我有疑問。讓我們考慮我創造了5個領域。我如何在創建時更改佔位符,以及如何獲得其價值? – user3383301

+0

好的。等待。我會用代碼更新我的答案以獲得它。 –

+0

查看我更新的答案,我會很樂意幫助你。謝謝@ user3383301 –

0

試試這個:

LinearLayout myRoot = (LinearLayout) findViewById(R.id.formLayout); 
LayoutInflater inf = LayoutInflater.from(yourContext); 
View child; 
for (int i = 0; i < 10; i++) { 
    child = inf.inflate(R.layout.your_added_layout, null); 
    child.setId("textView"+i); 
    // can set layoutparam if needed. 
    myRoot.addView(child); 
}