简体   繁体   中英

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.

So far these are the options:

  1. Set dp padding for each device
  2. Make View above this image and change it's padding for each device
  3. Calculate screen size in Java code and from the code set its locate in generics way. For example, set that image is located 1/5 screen height from top and apply this formula depending on current device's screen height.
  4. Similar to point 2., but instead of using fixed dp , I play around with layout_weight of elements in question

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.

A small example for the code approach - Lets say you want a View to be exactly 25 percent of screen width:

 WindowManager manager = (WindowManager) _context.getSystemService(Activity.WINDOW_SERVICE);
 int screenWidth = manager.getDefaultDisplay().getWidth();
 YOUR_VIEW.getLayoutParams().width = (int) (screenWidth * 0.25);

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