简体   繁体   中英

Android change size programmatically

I need to change the size of the buttons programmatically. I read some answers and I do the next to change the button size.

button.setLayoutParams(new RelativeLayout.LayoutParams(150, 50));

This code change the size correctly but the problem is that the button not stay in the position I want, it moves the button to the top left corner.

I need to change the button size but I don't want to change de position.

How can I fix this?

You have to use the existing RelativeLayout Rules as well,ie whether the item is center aligned,align parent left etc. Or you can make use of the existing Params and modify the width and height as follows the RelativeLayout rules will not be modified.

  RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) button.getLayoutParams();
        layoutParams.width=150;
        layoutParams.height=50;
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams();
params.width = 500;
params.height = 500;
button.setLayoutParams(params);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM