2016-11-17 59 views
0

我想创建一个布局窗体编程。对于这个例子,我想在我的表单中添加一个像textview一样的视图几次。我创建了另一个具有textview的布局,现在我想在当前布局中添加这个textview几次。我怎么能从另一个布局添加一个视图像textview到当前布局android

public class FileForm extends Activity { 

    LinearLayout LIN_Main; 
    TextView TV_GroupTitle; 
    LayoutInflater LYOTInf; 
    View VIW_AllItems; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_file_form); 
     LIN_Main = (LinearLayout) findViewById(R.id.LIN_Main); 
     LYOTInf = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     VIW_AllItems = LYOTInf.inflate(R.layout.item,null); 
     TV_GroupTitle = (TextView) VIW_AllItems.findViewById(R.id.TV_GroupTitle); 
     int INT_GroupNum = 0; 
     List<Integer> INT_EmNumInGroup = new ArrayList<Integer>();; 
     EmManager EMM = null; 
     EMM = EMM.getInstance();  

     Element EmNode = EMM.getEm(); 
     Element EmG = null; 
     Element EmE = null; 
     NodeList Group = null; 
     NodeList EmList = null; 
     Group = EmNode.getChildNodes(); 
     INT_GroupNum = Group.getLength(); 
     LIN_Main.removeAllViews(); 

     for (int i=0;i<INT_GroupNum;i++) 
     { 
      EmList = Group.item(i).getChildNodes(); 
      INT_EmNumInGroup.add(EmList.getLength()); 

      for (int j=0;j<INT_EmNumInGroup.get(i);j++) 
      { 
       EmE = (Element) EmList.item(j); 
       for(int att=0;att<Integer.parseInt(EmE.getAttribute("Num"));att++) 
       { 
        TV_GroupTitle.setText(EmE.getNodeName()); 
        LIN_Main.addView(TV_GroupTitle); 
       } 
      } 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.file_form, menu); 
     return true; 
    } 

} 

但有错误。

回答

1

您必须以编程方式创建的TextView(TextView的文本=新的TextView(上下文)),不与findviewById,你必须做的对,是这样的:

for (int j=0;j<INT_EmNumInGroup.get(i);j++) 
      { 

       EmE = (Element) EmList.item(j); 

       for(int att=0;att<Integer.parseInt(EmE.getAttribute("Num"));att++) 
       { 

        TextView TV_GroupTitle = new TextView(this) 

        TV_GroupTitle.setText(EmE.getNodeName()); 

        LIN_Main.addView(TV_GroupTitle); 

       } 

      } 
+0

感谢我的朋友 –

1

您可以膨胀布局往往你需要它

for(int att=0;att<Integer.parseInt(EmE.getAttribute("Num"));att++) { 
    TextView textView = (TextView)LYOTInf.inflate(R.layout.item,null); 
    LIN_Main.addView(textView); 
} 

或只是创造新的TextView对象编程

for(int att=0;att<Integer.parseInt(EmE.getAttribute("Num"));att++) { 
    TextView textView = new TextView(context); 
    //Configure the textView here (LayoutParams, ...) 
    LIN_Main.addView(textView); 
} 

只要确保每行都有一个新对象!你不能只是设置在同一对象上的文本,然后重新添加(像你这样)

for(int att=0;att<Integer.parseInt(EmE.getAttribute("Num"));att++) 
{ 
    TV_GroupTitle.setText(EmE.getNodeName()); 
    LIN_Main.addView(TV_GroupTitle); 
} 
+0

感谢我的朋友 –

+0

是的,你是对的。在ListView中,它可能在每行列表视图中多次添加布局的textview。在这种情况下,我可以做到这一点 –

相关问题