简体   繁体   中英

How to set same width and height of a button

I have a xml layout file in which I have a Button . The Height of the Button is set as match_parent . Now I want the Width to be the same as the Height of the Button to make a square button regardless of changing Height and Width of parent layout..

How about this:

Button yourBtn = (Button) findViewById.(R.id.yourBtn);

int btnSize=yourBtn.getLayoutParams().width;
yourBtn.setLayoutParams(new LayoutParams(btnSize, btnSize));

You have to do it programmatically. XML layout is only for static stuff.

After the button have been draw at least once on the screen you will resize it by setting a new LayoutParams to it with the desired values.

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