我想补充的TextView和编辑文本的5块如下的Android的TextView和EditText上编程
文本视图----编辑文本----文本视图
文本视图----编辑文字 - ---文本视图
文本视图----编辑文本----文本视图
文本视图----编辑文本----文本视图
文本视图----编辑文本--- - 文本视图
我试过以下内容:
LinearLayout rootLayout = (LinearLayout) findViewById(R.id.root_layout);
for (int i = 0; i < 6; i++) {
rootLayout.setOrientation(LinearLayout.HORIZONTAL);
TextView textView = new TextView(this);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1));
textView.setText("Text");
rootLayout.addView(textView);
EditText editText = new EditText(this);
editText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1));
rootLayout.addView (editText);
TextView addTextView = new TextView(this);
addTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1));
addTextView.setText("Additional Text");
rootLayout.addView(addTextViewtextView);
// TextView dividerLine = new TextView(this);
// rootLayout.setOrientation(LinearLayout.VERTICAL);
// rootLayout.addView(dividerLine);
使用上面的代码水平添加所有15(3 * 5)视图。当我取消注释最后三行时,所有视图都会垂直添加。看起来,布局是根据程序中最后一个setOrientation语句设置的。
不清楚是什么问题。 LinearLayout需要定位。 – Lino
为什么你要通过循环添加你可以使用包含布局? – Abhishek