1

我想添加一组元素(文本框、按钮、进度条和文本视图),并在 Android 中按下按钮时让它们动态显示。在每次按下按钮时,我都想创建一组元素并将它们放在相对布局中。这是我的oncreate方法:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // The add button to insert new downloading file
    add = (Button) findViewById(R.id.button_add_url);
    add.setOnClickListener(this);

    // Swapping between pause and resume
    pause = (Button) findViewById(R.id.button_pause_resume);
    pause.setOnClickListener(this);

    download = (Button) findViewById(R.id.button_download);
    download.setOnClickListener(this);

    // The progress is written here
    text = (TextView) findViewById(R.id.textView2);
    text.setTextColor(Color.WHITE);

    progress = (ProgressBar) findViewById(R.id.progressBar1);
}
4

1 回答 1

0

如果您将相对布局创建为包含这些元素的 xml,然后在单击按钮时,您可以像这样将该布局膨胀到主布局(在您的按钮 onClick 方法上),也许会更容易:

LinearLayout mainLayout = (LinearLayout)findViewById(R.id.personal_data_root);
RelativeLayout addLayout = (RelativeLayout)View.inflate(this, R.layout.layoutcontainingelementsouwanttoaddonclick, null);
mainLayout.addView(addLayout);
于 2013-03-22T16:46:18.987 回答