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

을,이 일을했다, 감사합니다! –

관련 문제