的Andr​​oid如何评估视图大小,以保持所有设备相同的比例 [英] Android how to evaluate view size to keep same proportion in all devices

查看:169
本文介绍了的Andr​​oid如何评估视图大小,以保持所有设备相同的比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种方法来保持匀称的所有设备上的视图大小。我尝试了很多数学计算(例如混合的屏幕尺寸,密度,百分比等),但没有发现任何解决方案,只是不同的结果测试的每个设备。

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.

让我present一个例子:假设我在SW-360dp测试的应用程序,最终我找到一个大小认为,工作原理

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;

这有可能会造成与大小约为屏幕宽度的1/3和1/2屏高视图的效果。

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

如何动态到达宽度和高度值,即让视图保持1/3宽度的纵横比为1/2的高度?至少我认为这就是当它说的文件是在暗示:从计算屏幕大小的百分比精确视图大小(或保证金,填充等)。

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".

还是我misinter preTED呢?

Or have I misinterpreted it?

任何帮助将AP preciated。

Any help will be appreciated.

推荐答案

您可以在设备屏幕尺寸有被确定的比例,

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

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

然后,

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

这篇关于的Andr​​oid如何评估视图大小,以保持所有设备相同的比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆