how to change size of button dynamic in android

。_饼干妹妹 提交于 2019-11-29 02:45:46
Flavio

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!