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 に答える