2016-11-23 168 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); 
}