Android and supporting multiple screens layouts

前端 未结 3 2011
清歌不尽
清歌不尽 2021-01-05 00:02

I\'m finishing off an Android app, all that remains is to adapt the UI layouts and graphics for multiple devices. I need particular elements placed in particular positions o

相关标签:
3条回答
  • 2021-01-05 00:36

    I think you're on the road to hell.

    Android runs on an enormous variety of devices, more every week, and many formats don't exist yet but will introduce new variables. So even if you succeed, one new device with a slightly different screen size, and your app will fail.

    It's a mistake to design for Android using specific screen resolutions, and is similar to the issues you'd find if you forced all pages to be the exact same size on the web, it rarely works well (e.g. even a tidy fixed-width site will fail miserably on mobile devices).

    Android has been designed to support all this variation, but if you try to get pixel-perfect absolute-positioned rendering on every screen size you are swimming against the tide. It is likely to be very painful, very time consuming and expensive, and likely to ultimately fail. Even if you succeed, how on earth will you test it on all these screen variants? It sounds like testing hell too.

    I STRONGLY recommend you accept you cannot do everything as exactly as you need to, and instead look at how to use ways of rendering objects fluidly, relative to each other, so the app looks good in all the different variations, using the different layouts for each group of resolutions to improve the experience on different size screens.

    0 讨论(0)
  • 2021-01-05 00:39

    Further to the answer/comments elsewhere on this page, I'd like to post another answer to my own question drawing attention to the type of screen resources that can be introduced. I'm not convinced this is made clear in the Android docs, but so far as drawables are concerned you can add screen size tags to drawable files on top of the dpi tag.

    For example, adding a folder called drawable-large-mdpi is valid and devices with large screens and medium resolution will pull resources from here if they can. Warning though, switching the order of the tags matters: declaring drawable-mdpi-large is an error.

    0 讨论(0)
  • 2021-01-05 00:43

    YES, that's possible:

    First you have to create a instance of the display-class. After that you get the display's width and heigth. Then you can check each resolution in a loop, by using the if operator and set the image you want.

    Example:

    ImageView iview=(ImageView)findViewById(R.id.imageView1);
    //here are the Bitmaps optimized for each screen resolution 
    Bitmap[] bitmaps={BitmapFactory.decodeResource(getResources(), R.drawable.icwvga800),BitmapFactory.decodeResource(getResources(), R.drawable.icwvga854),(...)};
    // In this list all supported screensizes are defined
    int[][] possibleScreenSolutions={{480,800},{480,854},{240,400},{240,432},{480,800},{480,854}};
    Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
    int[] ScreenSolution={display.getWidth(),display.getHeight()};
    // Compare the real screen solution with the all supported ones
    for(int i=0;i<possibleScreenSolutions.length;i++){
        if((ScreenSolution[0]==possibleScreenSolutions[i][0])&&(ScreenSolution[1]==possibleScreenSolutions[i][1])){
                iview.setImageBitmap(bitmaps[i]);// set the image
        }
    }
    

    I agree with Ollie C: It's too confusing to check all resolutions, but It's at least possible.

    I've tested it allready: It works.

    0 讨论(0)
提交回复
热议问题