2011-08-04 6 views
0

こんにちは、RelativeLayoutを持っていて、 "TableLayout"のような6つのボタンを表示したいと思っています。最後のボタンだけが表示されます。TableLayoutのようなRelativeLayout

私は6つのボタン(buttonsperscreen = 6)でアクティビティを試行しています。

私を助けることができますか?

コードは次のとおりです。

private void doLayoutWithButtons(int buttonsperscreen) { 

    // if total butons is % 2 == 0 
    if (buttonsperscreen % 2 == 0) { 
     // get display dimensions 
     DisplayMetrics d = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(d); 

     int w = d.widthPixels; 
     int h = d.heightPixels; 

     // calc size of buttons 
     int widthButton = w/2; // only two columns of buttons. 
     int heightButton = h/((buttonsperscreen)/2); // sample 100px/
                  // (6/2) = 3 

     int posx = 0; 
     int posy = 0; 

     for (int i = 1; i <= buttonsperscreen; i++) { 

      Button newButton = new Button(this); 

      newButton.setId(100 + i + 1); // ID of zero will not work 
      newButton.setText("Botão: " + i); 

      // atribuindo o tamanho do botão. 
      newButton.setHeight(heightButton); 
      newButton.setWidth(widthButton); 
      newButton.setId(1000 + i); 

      // positioning buttons... 
      RelativeLayout layout1 = new RelativeLayout(this); 

      // set layout with size of button 
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        widthButton, heightButton); 
      params.leftMargin = posx; 
      params.topMargin = posy; 

      newButton.setLayoutParams(params); 

      layout1.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
      layout1.addView(newButton); 
      setContentView(layout1); 

      // to calc positions x,y for each button for next iteration 
      if (posx + widthButton < w) { 
       posx = posx + widthButton; 
      } else { 
       posx = 0; 
       posy = posy + heightButton; 
      } 

     } 

    } 

} 

おかげ マテウス

+0

XMLを使用して希望するレイアウトを描画しますか? – Gopal

+0

リニアレイアウトのような相対レイアウトを使いたいのであれば、なぜリニアレイアウトを使っていないのですか? –

答えて

0

あなたが均等に画面全体に間隔を置いて配置ボタンを表示しようとしている場合は、コードでこれを行う必要はありません。レイアウト内でこれを指定することができます。

このように、6つのボタンの幅が水平に表示されます。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:orientation="horizontal"> 
     <Button android:layout_height="wrap_content" android:id="@+id/button1" 
      android:text="Button" android:layout_width="0dp" 
      android:layout_weight="1" /> 
     <Button android:layout_height="wrap_content" android:id="@+id/button2" 
      android:text="Button" android:layout_width="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 
    <LinearLayout android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:orientation="horizontal"> 
     <Button android:layout_height="wrap_content" android:id="@+id/button3" 
      android:text="Button" android:layout_width="0dp" 
      android:layout_weight="1" /> 
     <Button android:layout_height="wrap_content" android:id="@+id/button4" 
      android:text="Button" android:layout_width="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 
    <LinearLayout android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:orientation="horizontal"> 
     <Button android:layout_height="wrap_content" android:id="@+id/button5" 
      android:text="Button" android:layout_width="0dp" 
      android:layout_weight="1" /> 
     <Button android:layout_height="wrap_content" android:id="@+id/button6" 
      android:text="Button" android:layout_width="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</LinearLayout> 

限り、あなたは0dpこと、および1すべき重みを設定するlayout_widthを保つよう、Androidは自動的に関係なく、あなたが持っているボタンの何番目、等しくなるように幅を設定しません。

+0

いいえ...私はテーブルの6セルのような6つのボタンが必要です、理解できますか? – Mateus

+0

私はそれが何を意味するか正確にはわかりません。行ごとに2つのボタンがあるように調整しましたが、おそらく "表のセル"という意味を明確にすることができます。どこかにレイアウトの例がありますか? – pav

関連する問題