Java Android:无法更改动态按钮的宽度

Java Android:无法更改动态按钮的宽度,java,android,button,dynamic,layoutparams,Java,Android,Button,Dynamic,Layoutparams,我正在尝试制作固定尺寸的动态按钮。我可以改变高度,但不能改变宽度。按钮的宽度似乎与父按钮的宽度相匹配,它占据了整个屏幕的宽度。如果有两个按钮,则每个按钮的宽度为屏幕宽度的一半,当只有一个按钮时,按钮的宽度为屏幕宽度 TableLayout table = (TableLayout)findViewById(r.id.table); TableRow tableRow = new TableRow(this); table.addView(tableRow); Button button = ne

我正在尝试制作固定尺寸的动态按钮。我可以改变高度,但不能改变宽度。按钮的宽度似乎与父按钮的宽度相匹配,它占据了整个屏幕的宽度。如果有两个按钮,则每个按钮的宽度为屏幕宽度的一半,当只有一个按钮时,按钮的宽度为屏幕宽度

TableLayout table = (TableLayout)findViewById(r.id.table);
TableRow tableRow = new TableRow(this);
table.addView(tableRow);
Button button = new Button(this);
button.setLayoutParams(new TableRow.LayoutParams(30,30));
tableRow.addView(button);

谁能指出我哪里出了问题。

Instated button.setLayoutParamsnew TableRow.LayoutParams30,30;更改为以下代码更改按钮高度和宽度

 LayoutParams lp = new LayoutParams(40, 40);
    table.addView(button, lp);

只有这样,您才能动态更改按钮

首先,我得到按钮

 Button btn = (Button) findViewById(R.id.button1);
然后点击事件按钮我把这个代码只

TableRow.LayoutParams lp = new TableRow.LayoutParams(70, 70);
btn.setLayoutParams(lp);
而且效果很好。试试看。这会解决你的问题