我想在创建按钮时逐个动态地将按钮添加到网格布局,但在我的情况下,按钮是通过每次迭代逐个创建的,但它们一次添加到布局当循环迭代完成时。
//这里是代码...如何在运行时将按钮添加到布局
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-程序化可能有助于完整 –
您不应该暂停主线程来实现一个简单的动画。 –