how to change size of button dynamic in android

China☆狼群 提交于 2019-12-29 05:37:06

问题


I try setWidth() and setheight() method but it not work


回答1:


Try using LayoutParams. For example

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




回答2:


For me works perfect this:

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

    myButton.setLayoutParams(params);



回答3:


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);


来源:https://stackoverflow.com/questions/5472445/how-to-change-size-of-button-dynamic-in-android

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