2014-10-12 29 views
0

所以我创建标题和输入字段编程在循环中。因为它代表用下面的代码,所有的元素都出现揉成在彼此的顶部一起:父对齐程序生成的元素

RelativeLayout mRlayout = (RelativeLayout) findViewById(R.id.checkFieldsLayout); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.MATCH_PARENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
    Bundle extras = getIntent().getExtras(); 
    List<String> fields = extras.getStringArrayList("checkList"); 

    for (int i = 0; i < fields.size(); i++) { 
     TextView header = new TextView(this); // Pass it an Activity or Context 
     header.setText(fields.get(i).toString()); 
     header.setLayoutParams(params); 
     EditText field = new EditText(this); 
     field.setLayoutParams(params); 
     mRlayout.addView(header); 
     mRlayout.addView(field); 
    } 

如何编程奠定了这些元素,使他们与屏幕的宽度和停靠一个接一个的下面?谢谢!

回答

1

试试这个:

RelativeLayout mRlayout = (RelativeLayout) findViewById(R.id.checkFieldsLayout); 
Bundle extras = getIntent().getExtras(); 
List<String> fields = extras.getStringArrayList("checkList"); 

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.MATCH_PARENT, 
    RelativeLayout.LayoutParams.WRAP_CONTENT); 
params.addRule(RelativeLayout.ALIGN_PARENT_TOP); 


TextView header1 = new TextView(this); // Pass it an Activity or Context 
header.setText(fields.get(0).toString()); 
header.setLayoutParams(params); 
header1.setId(0); 

RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.MATCH_PARENT, 
    RelativeLayout.LayoutParams.WRAP_CONTENT); 
params2.addRule(RelativeLayout.BELOW,0); 

EditText field1 = new EditText(this); 
field.setLayoutParams(params2); 
field1.setId(fields.size()); 

mRlayout.addView(header); 
mRlayout.addView(field); 

for (int i = 1; i < fields.size(); i++) { 
    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.MATCH_PARENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 
    params2.addRule(RelativeLayout.BELOW,fields.size()+i -1); 

    TextView header = new TextView(this); // Pass it an Activity or Context 
    header.setText(fields.get(i).toString()); 
    header.setId(i); 
    header.setLayoutParams(params1); 

    RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.MATCH_PARENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 
    params2.addRule(RelativeLayout.BELOW, i); 

    EditText field = new EditText(this); 
    field.setLayoutParams(params2); 
    field.setId(fields.size()+i); 

    mRlayout.addView(header); 
    mRlayout.addView(field); 
} 
+0

经过一些修改,这个没有工作,谢谢! – 2014-10-12 12:38:23