2014-10-12 62 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