简体   繁体   English

如何调整按钮数组中按钮的大小

[英]How to resize buttons in button array

I am trying to resize the buttons in a button array I made, but I don't know how 我正在尝试调整我制作的按钮数组中按钮的大小,但是我不知道如何

Here's my code 这是我的代码

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

        }

    }

}

I don't Know if you know, but this will create 100 buttons, anyway: it can be done like this 我不知道你是否知道,但是无论如何这都会创建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

    }

If you're using the java.awt.Button class, which I'm assuming since I see no other specification here, you need to create a java.awt.Dimension object to specify the size. 如果您使用的是java.awt.Button类,因为我在这里没有看到其他说明,所以我假设该类是,您需要创建一个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);
    }
}

Change the values of the height and width as you see fit, should work. 可以根据需要更改高度和宽度的值。 You may also need to import java.awt.Dimension 您可能还需要导入java.awt.Dimension

You can resize button by modified its LayoutParams, like this: 您可以通过修改其LayoutParams来调整按钮的大小,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM