简体   繁体   中英

Set width/height percentage from code or use LinearLayout and weight?

My question is very simple. Which is more efficient: setting a views width's or height's percentage from JAVA code, by getting the phones pixel count and calculating the number of pixels needed for the desired width or height, or put everything in a LinearLayout and set the weight of the view.

NOTE: Sometimes the views need to have margins, which are also specified in percentages.

我更喜欢通过Linearlayout为子元素添加权重来添加你可以轻松和边缘。

Hey use this code context.getResources().getDisplayMetrics().widthPixels to get the width of your screen and context.getResources().getDisplayMetrics().heightPixels to get height and divide them in the desired ratio and set it to the view.

Hope this is what you want.

I think the weight is best for comparative distribution of views within LinearLayout .

its very flexible to use ..you can use it for the distribution of extra space within child or also create the weight oriented layout by setting the views height or width(whichever requried by you) to zero .

Nested weight are bad for performance

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