2013-07-19 53 views
1

我試圖調整按鈕陣列我做的按鈕,但我不知道如何調整按鈕按鈕配置

怎麼這裏是我的代碼

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.game); 
    final TableLayout container = (TableLayout) findViewById(R.id.tableLayout5); 

    Button btn[][] = new Button[10][10]; 

    for(int i = 0; i<10; i++){ 
     for(int j = 0; j<10; j++){ 
      btn [i][j] = new Button(this); 


      container.addView(btn[i][j],i); 

     } 

    } 

} 
+1

你什麼意思通過調整按鈕做?您的意思是設置尺寸或在設置後更改尺寸。 – Azad

回答

0

我不知道如果你知道,但是這將創建100個按鈕,反正: 它可以像這樣

public void onCreate(Bundle icicle) 
{ 
    super.onCreate(icicle); 
    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(width_of_button , height_of_button); 
    final TableLayout container = (TableLayout) findViewById(R.id.tableLayout5); 

    Button btn[][] = new Button[10][10]; 

    for(int i = 0; i<10; i++){ 
     for(int j = 0; j<10; j++){ 
      btn [i][j] = new Button(this); 
      btn.setText("Button "+i+":"+j); // if you need to trace which is which 



      container.addView(btn[i][j],i,lp);// add button with specified dims 

    } 
0

如果您使用的是java.awt.Button類,我假設,因爲我在這裏沒有看到其他規格來完成,Y您需要創建一個java.awt.Dimension對象來指定大小。

Dimension size=new Dimension(4,6); //where 4 is the width and 6 is the height, although odds are you will need something much bigger 

for(int i = 0; i<10; i++) 
{ 
    for(int j = 0; j<10; j++) 
    {  
     btn [i][j] = new Button(this); 
     btn.setSize(size); //inherited from Component 

     container.addView(btn[i][j],i); 
    } 
} 

如果您認爲合適,更改高度和寬度的值應該可以工作。 您可能還需要通過修改它的LayoutParams進口java.awt.Dimension中

+1

雖然代碼是java,但他使用的是我不稀薄的Android有java.awt.Button – FutureSci

0

您可以調整按鈕,像這樣:

TableLayout.LayoutParams params = new TableLayout.LayoutParams(); 
    params.height = 40; 
    params.width = 100; 
    button.setLayoutParams(params);