2016-07-25 25 views
0

ボタンをグリッドレイアウトに動的に追加したいのですが、私の場合、ボタンは各繰り返しで1つずつ作成されますが、レイアウトにまとめて追加されますループの反復が完了したとき。ここ
//はあなたの問題の簡単な解決策は、ViewクラスのpostDelayedを使用することができ、コード...ランタイムでレイアウトにボタンを追加する方法

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); 
     } 

     } 
+0

のように追加することができます。どのような問題が現在のコードを使用して取得? –

+0

これをチェックするhttp://stackoverflow.com/questions/13532084/set-rowspan-or-colspan-of-a-child-of-a-gridlayout-programmatically fullを助けるかもしれない –

+0

達成のためにメインスレッドを中断しないでくださいシンプルなアニメーション。 –

答えて

0

です。

for(int i = 0; i < 10; i++) { 
    final Button button = new Button(MainActivity.this); 
    ... 
    gridlayoyt.postDelayed(new Runnable() { 

     @Override 
     public void run(){ 
      gridlayout.addView(button); 
     } 

    }, i * 1000); 

} 
+0

これは私が欲しいものです – Arsalan

0

あなたは、あなたの質問は明らかではないが、この

LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags); 
layout.setOrientation(LinearLayout.VERTICAL); //Can also be done in xml by android:orientation="vertical" 

for (int i = 0; i < 3; i++) { 
    LinearLayout row = new LinearLayout(this); 
    row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

    for (int j = 0; j < 4; j++ { 
     Button btnTag = new Button(this); 
     btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     btnTag.setText("Button " + (j + 1 + (i * 4)); 
     btnTag.setId(j + 1 + (i * 4)); 
     row.addView(btnTag); 
    } 

    layout.addView(row); 
} 
関連する問題