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:
dp
padding for each device View
above this image and change it's padding for each device 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. 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:
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.