我想在創建按鈕時逐個動態地將按鈕添加到網格佈局,但在我的情況下,按鈕是通過每次迭代逐個創建的,但它們一次添加到佈局當循環迭代完成時。
//這裏是代碼...如何在運行時將按鈕添加到佈局
public void Add_Button(View view){
final MediaPlayer mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.button_sound);
gridlayout= (GridLayout) findViewById(R.id.layout);
animation= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.scale_button);
for(int i=0;i<10;i++) {
Button button = new Button(MainActivity.this);
button.setText(i+1 + "");
button.setPadding(10,10,10,10);
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.height=70;
params.width=70;
params.topMargin = 10;
params.leftMargin = 10;
*//after one 1 second i want to add button*
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
button.setLayoutParams(params);
button.setAnimation(animation);
mediaPlayer.start();
gridlayout.addView(button);
}
}
你的問題不明確。使用當前代碼有什麼問題? –
檢查此問題http://stackoverflow.com/questions/13532084/set-rowspan-or-colspan-of-a-child-of-a-gridlayout-程序化可能有助於完整 –
您不應該暫停主線程來實現一個簡單的動畫。 –