Java 如何调整按钮数组中按钮的大小
我正在尝试调整我制作的按钮阵列中的按钮大小,但我不知道如何调整 这是我的密码Java 如何调整按钮数组中按钮的大小,java,android,button,Java,Android,Button,我正在尝试调整我制作的按钮阵列中的按钮大小,但我不知道如何调整 这是我的密码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.game); final TableLayout container = (TableLayout) findViewById(R.id.tableLa
@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);
}
}
}
我不知道您是否知道,但无论如何,这将创建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
}
如果您使用的是这个类(我假设在这里没有看到其他规范),那么您需要创建一个对象来指定大小
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);
}
}
如果您认为合适,请更改高度和宽度的值。
您可能还需要导入java.awt.Dimension您可以通过修改其布局参数来调整按钮的大小,如下所示:
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.height = 40;
params.width = 100;
button.setLayoutParams(params);
调整按钮的大小是什么意思?你的意思是设置大小或在设置后更改大小。虽然代码是java,但他使用的是Android,我不认为它有java.awt.Button