Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

layout - How to support all the different resolutions of android products

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

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.

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

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

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;
    }
}

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?

Thanks a lot for your information!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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

and..

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...