对于布局中的子视图的循环

时间:2015-12-29 10:03:51

标签: android android-layout for-loop

由于我们在动态循环下面的视图时遇到问题,因此请求您查看相同内容并让我们知道任何有用且可靠的解决方案,这对我来说真的很有帮助。

Layout.xml

 <RelativeLayout
        android:id="@+id/ll2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll"
        >

        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Meda name"
            android:textColor="#000"
            android:textColorHint="#000" />

        <EditText
            android:id="@+id/editText3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="1"
            android:layout_toRightOf="@+id/editText2"
            android:textColor="#000"
            android:textColorHint="#000" />

        <EditText
            android:id="@+id/editText4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="tick"
            android:layout_toRightOf="@+id/editText3"
            android:textColor="#000"
            android:textColorHint="#000" />

        <EditText
            android:id="@+id/editText5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="tick"
            android:layout_toRightOf="@+id/editText4"
            android:textColor="#000"
            android:textColorHint="#000" />

    </RelativeLayout>

Code.java

 for (int i = 0; i < 4; i++) {
            RelativeLayoutll2 = (RelativeLayout) findViewById(R.id.ll2);
            EditText testname = new EditText(getApplicationContext());
            testname.setText("Med");
            testname.setTextColor(000);
            EditText testname1 = new EditText(getApplicationContext());
            testname.setText("Hello");
            testname.setTextColor(000);
            EditText testname3 = new EditText(getApplicationContext());
            testname.setText("Hello");
            testname.setTextColor(000);
            EditText testname2 = new EditText(getApplicationContext());
            testname.setText("Hello");
            testname.setTextColor(000);
            ll2.addView(testname);
            ll2.addView(testname1);
            ll2.addView(testname3);
            ll2.addView(testname2);
        }

请参阅此图片。当我在Spinner中选择必须显示该字段数的数字时,我希望如此。

image

1 个答案:

答案 0 :(得分:0)

LinearLayout parentLayout = (LinearLayout)findViewById(R.id.ll3);
        LayoutInflater layoutInflater = getLayoutInflater();
        View view;

        for (int i = 0; i < 4; i++) {
            view = layoutInflater.inflate(R.layout.text_layout, parentLayout, false);
            parentLayout.addView(view);
        }