2017-02-26 38 views
0

我想补充的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语句设置的。

+0

不清楚是什么问题。 LinearLayout需要定位。 – Lino

+0

为什么你要通过循环添加你可以使用包含布局? – Abhishek

回答

2
LinearLayout rootLayout = (LinearLayout) findViewById(R.id.root_layout); 
rootLayout.setOrientation(LinearLayout.VERTICAL); 
//this layout still needs to be vertical to hold the children. 
for (int i = 0; i < 6; i++) { 

    //make a new horizontal LinearLayout each time to hold the children. 
    LinearLayout temp = new LinearLayout(this); 
    temp.setOrientation(LinearLayout.HORIZONTAL); 
    temp.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
     LinearLayout.LayoutParams.WRAP_CONTENT,1)); 

    TextView textView = new TextView(this); 
    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT,1)); 
    textView.setText("Text"); 
    temp.addView(textView); //add them to this temporary layout. 

    EditText editText = new EditText(this); 
    editText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT,1)); 
    temp.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"); 
    temp.addView(addTextViewtextView); 

    rootLayout.addView(temp); 

通过这种方式,可以在里面添加一个多个线性布局。因此,基本上,对于每组TextView s,您正在制作一个单独的LinearLayout,然后将这些布局中的每一个添加到仍然垂直方向的主要LinearLayout

+0

谢谢!这正是我需要的。 – abhikush

+0

@abhikush很棒:) –

1

你的代码应该遵循这种方式。

  1. findViewById()的根布局设置它的方向垂直。

  2. 开始的for循环

  3. 采取线性布置,设置取向为水平 3.1添加文本视图 3.2附加编辑文本 3.3添加文本视图

  4. 此第三阶线性布局添加到根布局。

  5. For循环停止。

+0

这有效,但我在第3步感到困惑,而我接受的答案对我来说很明确。 – abhikush