[英]Best way to position image in the same spot on all devices?
I have realized that I am not 100% sure what is the best way to position some UI element so that it appears on the same spot on all devices. 我已经意识到我不确定100%确定某些UI元素的最佳方法是什么,以便使其在所有设备上都显示在同一位置。
So far these are the options: 到目前为止,这些选项是:
dp
padding for each device dp
填充 View
above this image and change it's padding for each device View
并更改每个设备的填充 Java code
and from the code set its locate in generics way. Java code
计算屏幕大小,并从代码集中以通用方式确定其位置。 For example, set that image is located 1/5 screen height from top and apply this formula depending on current device's screen height. dp
, I play around with layout_weight
of elements in question dp
,而是使用有问题的元素的layout_weight
What seems to be the best way? 最好的方法是什么?
Is there a better way which I did not mention here? 有没有我在这里没有提到的更好的方法?
Thanks 谢谢
The two best options are as follows: 最好的两个选择如下:
layout_weight
for your views and weight_sum
for the parent view. layout_weight
用于您的视图,并将weight_sum
用于父视图。 A small example for the code approach - Lets say you want a View to be exactly 25 percent of screen width: 一个简单的代码示例-假设您希望View恰好是屏幕宽度的25%:
WindowManager manager = (WindowManager) _context.getSystemService(Activity.WINDOW_SERVICE);
int screenWidth = manager.getDefaultDisplay().getWidth();
YOUR_VIEW.getLayoutParams().width = (int) (screenWidth * 0.25);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.