开发者

how to change size of button dynamic in android

I try setWidth(开发者_如何学C) and setheight() method but it not work


Try using LayoutParams. For example

button.setLayoutParams (new LayoutParams(50, LayoutParams.WRAP_CONTENT)


For me works perfect this:

    ViewGroup.LayoutParams params = myButton.getLayoutParams();
    //Button new width
    params.width = 400;

    myButton.setLayoutParams(params);


I found this to be the best solution because it also allows the button to be repositioned at the same time by simply changing its margin:

int buttonWidth = (int) (screenWidth / 3.0);
RelativeLayout.LayoutParams params = (LayoutParams) myButton.getLayoutParams();
params.width = buttonWidth;
params.leftMargin = buttonWidth;
myButton.setLayoutParams(params);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜