简体   繁体   English

在所有设备上将图像定位在同一位置的最佳方法?

[英]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: 到目前为止,这些选项是:

  1. Set dp padding for each device 为每个设备设置dp填充
  2. Make View above this image and change it's padding for each device 在该图像上方制作“ View并更改每个设备的填充
  3. Calculate screen size in 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. 例如,将图像设置为距顶部屏幕高度的1/5,并根据当前设备的屏幕高度应用此公式。
  4. Similar to point 2., but instead of using fixed dp , I play around with layout_weight of elements in question 与第2点类似,但是我没有使用固定的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: 最好的两个选择如下:

  1. In code - calculate screen size and work with percentage of the screen. 在代码中-计算屏幕尺寸并使用屏幕百分比。
  2. Use 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.

相关问题 如何将图像定位在所有android设备的同一位置? - How to position images at the same spot all android devices? 所有Android设备上的UI元素(按钮,图像按钮)的位置相同 - Same position for UI elements (buttons, image buttons) on all android devices 调整位图在画布中的大小和位置的最佳方法,以便它们在不同的设备中看起来相同? - Best way to resize and position bitmaps in a canvas so they look the same in different devices? 调整图像大小以使所有设备看起来相同 - Resizing an image to look the same across all devices 如何在所有设备上看起来相同的边距处放置视图? - How to position the view with margin that will look same for all devices? 在运行相同应用程序的设备之间发送消息的最佳方法? - Best way to send messages between devices running the same application? 如何在Android中保持所有设备尺寸的图像位置和大小? - how to maintain the Image Position and Size for all size of Devices in android? 一次将消息发送到所有用户设备的最佳方法是什么? - What is the best way to send a message to all my users' devices at once? 所有屏幕分辨率下图像上按钮的位置相同 - Same position of Buttons on Image with all screen resolutions 在多个屏幕上的同一位置显示图像| Android的 - Display image in the same spot on multiple screens | Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM