2013-03-05 61 views
0

enter image description here我该如何放置我的按钮?

我想把这样的数字按钮。这是我的代码:

LinearLayout layout = new LinearLayout(this); 
     layout.setOrientation(LinearLayout.VERTICAL); 

     LinearLayout layout2 = new LinearLayout(this); 
     layout2.setOrientation(LinearLayout.HORIZONTAL); 

     TextView titleView = new TextView(this); 
     titleView.setText("Hello World!"); 
     layout.addView(titleView); 

     android.widget.LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, 1); 
     android.widget.TableLayout.LayoutParams params = new TableLayout.LayoutParams(
       LayoutParams.MATCH_PARENT, LayoutParams.FILL_PARENT, 1); 

     Button btnConnect = new Button(this); 
     btnConnect.setText("Connect"); 
     btnConnect.setLayoutParams(param); 
     layout2.addView(btnConnect); 

     Button btnDisconnect = new Button(this); 
     btnDisconnect.setText("Disconnect"); 
     layout2.addView(btnDisconnect); 
     btnDisconnect.setLayoutParams(param); 


     layout.addView(layout2); 

     TableLayout tblLayout = new TableLayout(this); 
     tblLayout.setOrientation(TableLayout.HORIZONTAL); 
     TableRow tblrow = null; 



     for (int i = 1; i <= 9; i++) { 
      if (i % 3 == 1) { 
       tblrow = new TableRow(this); 
       tblLayout.addView(tblrow); 

      } 

      Button b = new Button(this); 
      b.setText("" + i); 
      tblrow.addView(b); 
     } 




     TableRow tr = new TableRow(this); 
     Button btnZero = new Button(this); 
     btnZero.setText("0"); 
     Button btnHash = new Button(this); 
     btnHash.setText("#"); 
     Button btnStar = new Button(this); 
     btnStar.setText("*"); 
     tr.addView(btnZero); 
     tr.addView(btnHash); 
     tr.addView(btnStar); 
     tblLayout.addView(tr); 




     layout.addView(tblLayout); 
     setContentView(layout); 

这符合以下标准:

enter image description here

为了使我的专栏这样。我用这个代码:

android.widget.TableLayout.LayoutParams params = new TableLayout.LayoutParams(
       LayoutParams.MATCH_PARENT, LayoutParams.FILL_PARENT, 1); 
tblLayout.setLayoutParams(param); 

但没有变化。我需要做什么?这是Layoutparams不足以做到这一点?

+0

您可以在布局中放置布局。例如,你可以用“LinearLayout”水平放置顶部按钮,其余部分用“GridLayout”放置。 – 2013-03-05 22:17:53

+0

是的,你是对的我需要使用TableLayout。这是必须的 – snnlaynnkrdsm 2013-03-05 22:18:45

+2

是否有你不在XML文件中这样做的原因?看起来像矫枉过正,在代码中动态创建它。 – dymmeh 2013-03-05 22:29:33

回答

2

你需要设置的layoutlayout2tblLayout,每个tblRow,并且trMATCH_PARENT宽度和每个按钮必须有平等的layout_weight

+0

LayoutParams.FILL_PARENT,1最后1是宽度不是它,但不工作 – snnlaynnkrdsm 2013-03-05 22:23:19

相关问题