试试Following,就当做基础Demo吧:
public class ABCActivity extends Activity {
int myid = 2001;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout scrollHolder = new RelativeLayout(this);
scrollHolder.setId(++myid);
RelativeLayout.LayoutParams scrollHolderParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
scrollHolder.setLayoutParams(scrollHolderParams);
scrollHolder.setBackgroundColor(Color.DKGRAY);
scrollHolder.setGravity(Gravity.CENTER_VERTICAL);
// ScrollView
ScrollView scroll = new ScrollView(this);
scroll.setId(++myid);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
scroll.setBackgroundColor(Color.GREEN);
// RelativeLayout
RelativeLayout etHolder = new RelativeLayout(this);
etHolder.setId(++myid);
RelativeLayout.LayoutParams etHolderParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
etHolder.setLayoutParams(etHolderParams);
etHolder.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
etHolder.setPadding(0, 0, 0, 25);
// Creating EditTextes and Adding to etHolder AS you have done
RelativeLayout rl2 = addEditText(etHolder, null, 1);
RelativeLayout rl3 = addEditText(etHolder, rl2, 2);
RelativeLayout rl4 = addEditText(etHolder, rl3, 3);
RelativeLayout rl5 = addEditText(etHolder, rl4, 4);
RelativeLayout rl6 = addEditText(etHolder, rl5, 5);
RelativeLayout rl7 = addEditText(etHolder, rl6, 6);
RelativeLayout rl8 = addEditText(etHolder, rl7, 7);
RelativeLayout rl9 = addEditText(etHolder, rl8, 8);
RelativeLayout rl10 = addEditText(etHolder, rl9, 9);
RelativeLayout rl11 = addEditText(etHolder, rl10, 10);
RelativeLayout rl12 = addEditText(etHolder, rl11, 11);
RelativeLayout rl13 = addEditText(etHolder, rl12, 12);
scroll.addView(etHolder); // adding RelativeLayout = etHolder to ScrollView = scroll
scrollHolder.addView(scroll); // adding ScrollView = scroll to RelativeLayout = scrollHolder
setContentView(scrollHolder);
}
// Method as you have done
private RelativeLayout addEditText(RelativeLayout objRLContent,
RelativeLayout layoutAbove, int i) {
RelativeLayout objRLEditText = new RelativeLayout(this);
objRLEditText.setId(1100 + i);
RelativeLayout.LayoutParams objRLEditTextParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (layoutAbove != null)
objRLEditTextParams.addRule(RelativeLayout.BELOW,
layoutAbove.getId());
objRLEditText.setLayoutParams(objRLEditTextParams);
objRLEditText.setPadding(8, 2, 8, 2);
Button objBtn = new Button(this);
objBtn.setId(1300 + i);
RelativeLayout.LayoutParams objETDataParams = new RelativeLayout.LayoutParams(
200, LayoutParams.WRAP_CONTENT);
objBtn.setLayoutParams(objETDataParams);
objBtn.setPadding(8, 0, 8, 0);
objBtn.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
objBtn.setText("" + i);
objBtn.setSingleLine(true);
objRLEditText.addView(objBtn);
objRLContent.addView(objRLEditText);
return objRLEditText;
}
}