需要动态添加布局

时间:2016-11-23 04:42:01

标签: android android-layout

这是我的布局,我将线性布局嵌套在另一个线性布局中,该布局嵌套在Scroll视图中。现在我想在android:id="@+id/formLayout"内动态添加一个线性布局(我甚至可以加10),即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>

有人可以帮我这个吗?

2 个答案:

答案 0 :(得分: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

希望这会对你有所帮助。谢谢

答案 1 :(得分: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);
}