Android how to evaluate view size to keep same proportion in all devices

余生颓废 提交于 2019-12-12 06:10:15

问题


I'm searching for a way to keep view size proportioned on all devices. I experimented with many maths calculations (e.g. mixing screen size, density, percentage, etc.) but haven't found any solutions, just different results for each device tested.

Let me present an example: Suppose I'm testing an app on a sw-360dp and eventually I find a size view that works.

relativeLayoutParams.width  = 150;

relativeLayoutParams.height = 250;

This has the effect of creating a view with size about 1/3 of screen width and 1/2 screen height.

How can I dynamically arrive at value for width and height, that lets the view keep the aspect ratio of 1/3 width to 1/2 height? At least I think that's what the documentation is suggesting when it says: "calculate exact view size (or margin, padding, etc.) from a percentage of screen size".

Or have I misinterpreted it?

Any help will be appreciated.


回答1:


You can get the device screen size and there by define the ratio,

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);

Then,

    width = (int) size.x * 1/3 ; 
    height = (int) size.y * 1/2 ; 


来源:https://stackoverflow.com/questions/29424627/android-how-to-evaluate-view-size-to-keep-same-proportion-in-all-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!