如何支持Android产品的所有不同的决议决议、不同、产品、Android

2023-09-12 07:30:55 作者:并不是与生俱来の花心

在不同的Andr​​oid产品的所有不同的分辨率让我疯了。

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

我的第一个Android的应用程序,我写的是这样设计,它支持常用的三种分辨率:240×​​320(LDPI),小320x480(MDPI)和480×800(华电国际)。在为480x854没有做任何伤害的布局,因为它具有相同的宽度为480×800。

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.

我也购买了如下设备来测试我的Andr​​oid应用: 三星Galaxy欧洲(LDPI) HTC Desire Z的(华电国际)

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

幸运的是我的女朋友有一个HTC野火S(MDPI),所以我已经得到了大多数决议范围之内。

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

但今天,我的兄弟下载了他的新的HTC Sensation,这种具有另一种分辨率540x960(华电国际?)我的应用程序。并没有显示我的应用程序,因为它应该而且可能是大多数平板电脑将不显示它正确两种。

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和华电国际。这工作正常,对于上述决议,​​但是这取决于屏幕分辨率ICW屏幕大小和失败,例如宏达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

我读过支持多画面的多次,但没有找到一个明确的答案,这问题。

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.