Android 布局中子视图的For循环
由于我们在动态循环下面的视图时遇到了问题,因此请您研究这些问题,并让我们知道任何好的、可靠的解决方案,这对我非常有帮助 Layout.xmlAndroid 布局中子视图的For循环,android,android-layout,for-loop,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"
<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);
}
for(int i=0;i<4;i++){
RelativeLayoutll2=(RelativeLayout)findViewById(R.id.ll2);
EditText testname=新的EditText(getApplicationContext());
testname.setText(“Med”);
testname.setTextColor(000);
EditText testname1=新的EditText(getApplicationContext());
setText(“你好”);
testname.setTextColor(000);
EditText testname3=新的EditText(getApplicationContext());
setText(“你好”);
testname.setTextColor(000);
EditText testname2=新的EditText(getApplicationContext());
setText(“你好”);
testname.setTextColor(000);
ll2.addView(testname);
ll2.addView(testname1);
ll2.addView(testname3);
ll2.addView(testname2);
}
请参考这张图片。当我在微调器
中选择数字时,我希望是这样的,它必须显示字段的数量
LinearLayout parentLayout=(LinearLayout)findViewById(R.id.ll3);
LayoutInflater LayoutInflater=getLayoutInflater();
视图;
对于(int i=0;i<4;i++){
视图=布局更平坦。充气(R.layout.text\u布局,parentLayout,false);
parentLayout.addView(视图);
}
为什么不使用ListView呢?我已经动态完成了,先生,我想尝试不同的方式,这很好。干得好,那就把你的答案贴出来吧
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);
}