简体   繁体   English

从代码设置宽度/高度百分比或使用LinearLayout和重量?

[英]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. 哪个更有效:从JAVA代码设置视图宽度或高度百分比,通过获取手机像素数并计算所需宽度或高度所需的像素数,或将所有内容放入LinearLayout并设置视图的权重。

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. 嘿使用此代码context.getResources().getDisplayMetrics().widthPixels来获取屏幕的宽度和context.getResources().getDisplayMetrics().heightPixels获取高度并将其除以所需的比例并将其设置为视图。

Hope this is what you want. 希望这是你想要的。

I think the weight is best for comparative distribution of views within LinearLayout . 我认为权重最适合在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 嵌套的重量对性能不利

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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