如何支持所有不同分辨率的安卓产品 [英] How to support all the different resolutions of android products

查看:19
本文介绍了如何支持所有不同分辨率的安卓产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不同 Android 产品的所有不同分辨率都让我抓狂.

All the different resolutions of the different Android products are driving me nuts.

我编写的第一个 Android 应用程序设计为支持三种常用的分辨率:240x320 (LDPI)、320x480 (MDPI) 和 480x800 (HDPI).480x854 对布局没有任何损害,因为它与 480x800 具有相同的宽度.

My first android app that I wrote was designed so it supported the three commonly used resolutions: 240x320 (LDPI), 320x480 (MDPI) and 480x800 (HDPI). The 480x854 didn't do any harm to the layout because it has the same width as 480x800.

我还购买了以下设备来测试我的 Android 应用:三星银河欧洲 (LDPI)HTC Desire Z (HDPI)

I've also bought the following devices to test my android apps on: Samsung Galaxy Europe (LDPI) HTC Desire Z (HDPI)

幸运的是,我女朋友有一台 HTC Wildfire S (MDPI),所以我已经涵盖了大多数分辨率.

Luckily my girlfriend has a HTC Wildfire S (MDPI) so I've got most resolutions covered.

但是今天,我哥哥在他的新 HTC Sensation 上下载了我的应用程序,它还有另一个分辨率 540x960(HDPI?).哪个没有按应有的方式显示我的应用,而且可能大多数平板电脑也无法正确显示.

But today, my brother downloaded my app on his new HTC Sensation which has yet another resolution 540x960 (HDPI?). Which didn't show my app as it should and probably the most tablets won't show it correctly either.

我在我的第一个应用程序中所做的是读出密度,然后设置参数:

What I've did with my first app was read out the density and then set the parameters:

public void set_ui_parameters() {
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    if(metrics.densityDpi == DisplayMetrics.DENSITY_HIGH){
        textSize   = 35;    
        timeWidth  = 80;
        dayWidth   = 110;
        moneyWidth = 50;
    } else if(metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM){
        textSize   = 35;    
        timeWidth  = 53;
        dayWidth   = 73;
        moneyWidth = 33;
    } else if(metrics.densityDpi == DisplayMetrics.DENSITY_LOW){
        textSize   = 28;
        timeWidth  = 40;
        dayWidth   = 55;
        moneyWidth = 25;
    }
}

除了参数之外,我还为 LDPI、MDPI 和 HDPI 创建了可绘制对象.这适用于上述分辨率,但这取决于屏幕分辨率 i.c.w.屏幕尺寸和失败,例如 HTC sensatoin 540x960.

Besides the parameters I've also created drawables for LDPI, MDPI and HDPI. This works fine for the resolutions described above, but this depends on the screen resolution i.c.w. screen size and fails for, for example the HTC sensatoin with 540x960.

我知道并非所有决议都经常使用,但我想支持尽可能多的决议.屏幕尺寸和密度的统计数据

I know that not all the resolutions are used that often, but I would like to support as many as possible. Stats of Screen Sizes and Densities

我已经多次阅读Supporting Multiple Screens 但没有找到这个问题"的明确答案.

I've read Supporting Multiple Screens multiple times but didn't found a clear answer to this "problem".

那么我应该读出分辨率并根据分辨率而不是密度来设置参数吗?这是一个聪明的做法,或者你如何应对?

So should I read out the resolution and set the parameters according to the resolutions instead of density? Is this a smart thing to do or how do you cope with this?

非常感谢您的信息!

推荐答案

您不必这样做来支持不同的密度.您要做的是创建不同的资源文件夹:

You don't have to do that to support different densities. What you do is create different resources folders:

res/values-ldpi/dimens.xml
res/values-mdpi/dimens.xml
res/values-hdpi/dimens.xml

然后Android将决定使用哪个文件.你可以有类似的东西:

Then Android will decide which file to use. You can have something like:

<!-- in values-ldpi/dimens.xml -->
<dimen name="textSize">25dip</dimen>

还有……

<!-- in values-mdpi/dimens.xml -->
<dimen name="textSize">20dip</dimen>

等等.而且您不应该关心分辨率……有很多不同的分辨率大小,因此根据此做出决定将是地狱.

etc. And you shouldn't care about resolution... there are a lot of different resolutions sizes so it would be a hell to take decisions based on that.

此外,如果您使用 dp 而不是像素,您几乎不需要为每个密度创建不同尺寸的文件.当然,有时您必须这样做,但这取决于应用程序.

Also, if you use dp instead of pixels, you hardly ever will have to create different dimensions files for each density. Of course, sometimes you have to, but it depends on the app.

这篇关于如何支持所有不同分辨率的安卓产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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