Android : 无法更改动态按钮的宽度
Android : Unable to change width of dynamic button
我正在尝试制作固定尺寸的动态按钮。我能够改变高度但无法改变宽度。按钮的宽度好像是MATCH_PARENT(占了整个屏幕的宽度,如果有两个按钮,每个按钮的宽度是屏幕宽度的一半,只有一个按钮时,按钮的宽度是屏幕宽度)。
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);
谁能指出我哪里错了。
已声明 "button.setLayoutParams(new TableRow.LayoutParams(30,30));" 更改为以下代码更改按钮的高度和宽度。
LayoutParams lp = new LayoutParams(40, 40);
table.addView(button, lp);
动态更改按钮只需要做这个
首先我得到按钮
Button btn = (Button) findViewById(R.id.button1);
然后Button Click事件我把This Code Only
TableRow.LayoutParams lp = new TableRow.LayoutParams(70, 70);
btn.setLayoutParams(lp);
并且工作正常。尝试一下。将解决您的问题
我正在尝试制作固定尺寸的动态按钮。我能够改变高度但无法改变宽度。按钮的宽度好像是MATCH_PARENT(占了整个屏幕的宽度,如果有两个按钮,每个按钮的宽度是屏幕宽度的一半,只有一个按钮时,按钮的宽度是屏幕宽度)。
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);
谁能指出我哪里错了。
已声明 "button.setLayoutParams(new TableRow.LayoutParams(30,30));" 更改为以下代码更改按钮的高度和宽度。
LayoutParams lp = new LayoutParams(40, 40);
table.addView(button, lp);
动态更改按钮只需要做这个
首先我得到按钮
Button btn = (Button) findViewById(R.id.button1);
然后Button Click事件我把This Code Only
TableRow.LayoutParams lp = new TableRow.LayoutParams(70, 70);
btn.setLayoutParams(lp);
并且工作正常。尝试一下。将解决您的问题