我尝试使用setWidth()和setheight()方法,但它不起作用
答案 0 :(得分:12)
尝试使用LayoutParams。例如
button.setLayoutParams (new LayoutParams(50, LayoutParams.WRAP_CONTENT)
答案 1 :(得分:10)
对我而言,完美无缺:
ViewGroup.LayoutParams params = myButton.getLayoutParams();
//Button new width
params.width = 400;
myButton.setLayoutParams(params);
答案 2 :(得分:2)
我发现这是最好的解决方案,因为它还允许通过简单地更改其边距来同时重新定位按钮:
int buttonWidth = (int) (screenWidth / 3.0);
RelativeLayout.LayoutParams params = (LayoutParams) myButton.getLayoutParams();
params.width = buttonWidth;
params.leftMargin = buttonWidth;
myButton.setLayoutParams(params);