Supporting multiple screen size - Android

好久不见. 提交于 2019-12-27 12:06:02

问题


I am going to develop new application in Android. This application should only work in portrait (even for tablet). Also the UI and layout design should be similar on phones and tablet. We can't change the layout design for tablet as it has huge area to use. We have to stretch all the images to match phones. We can use nine patch. But I am little bit confused of using images in multiple drawables.

As per my analysis (may be wrong.. : ) ) the screens are divided into density and sizes. We can use the scaling ratio of 3:4:6:8. But this ratio is based on the density. But in my case I have to stretch the entire UI to fill the Tablet screen.

So what are the drawables that can be used for a app like this which can support multiple devices. And what are the screen sizes for which we have to design.

And this application needs nearly 100 layouts. So I am planning to maintain single layout and designing the layout using weight for each layout instead of using dimension.

Also if I used multiple APKs to support different screen size what are the drawables used to support 1. Small and Normal 2. Large 3. Xlarge


回答1:


I just did something very similar. To stretch the app without creating new layouts I used dimensions set in XML

res/values/dimensions.xml
res/values-sw600dp/dimensions.xml -> 7+ inches
res/values-sw720dp/dimensions.xml -> 10+ inches

Dimensions are resources files:

<dimen name="default_padding">11dp</dimen>

You can increase the dimensions by about 30% in the 600 and 720 file. Then simply used @dimen/default_padding in your layout and it will be scaled

Regarding images, either you make sure you have all your assets in all densities, or you set fixed size to you ImageView's and appropriate scaleType




回答2:


Firstly, you do NOT want to create multiple APKs to support multiple screen densities. Android provides all of the framework you need to support everything within one build, you just need to create the right resource hierarchy drawables with your desired densities.

So what exactly do you need... based on your question the following:

  • portrait mode: you can specify this in each Activity declared in your AndroidManifest file using the following:

    <activity android:name=".MainActivity" 
        android:label="@string/app_name"
        android:configChanges="orientation" > 
        ...
    </activity>
    

    NOTE: Per the Android docs, if you're targeting API >= 13, and you use the android:configChanges attribute you should also use the android:screenSize attribute to help specify size changes.

  • dimension sizes for various screens: as touched upon, this can also be handled in resources. This is your best way to use one common layout file but configure the layout for use on numerous devices. See http://developer.android.com/guide/topics/resources/more-resources.html#Dimension for how to use dimensions if you're unfamiliar

  • drawables: it sounds like this is the crux of your question. As you mentioned, using nine-patches will help you reduce your app footprint and fill in extra space (see here and here for more on nine-patches). The sizes you should support and the densities needed for those sizes are discussed in great detail in Android design docs, so much detail I could not even do it justice rehashing it here. I've provided links below to as many places as I could remember that this is discussed.

Good luck!

Here are links to Android design docs that you will find useful (some of which have been mentioned):

  • http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
  • http://developer.android.com/training/multiscreen/index.html
  • http://developer.android.com/guide/practices/screens_support.html
  • http://developer.android.com/design/style/devices-displays.html



回答3:


In addition to the pixel density specific folders, you can specify screen-size specific folders

drawable/
drawable-large/
drawable-xlarge/

drawable-hdpi/
drawable-large-hdpi/
drawable-xlarge-hdpi/

drawable-xhdpi/
drawable-large-xhdpi/
drawable-xlarge-xhdpi/

So you could design scale appropriate graphics for the various screen sizes and densities. Please note that a give screen size category (e.g. "large") will only give you a rough idea as to the actual device pixel dimensions of the device, but you'll get good guidelines for min/max dp ranges.

For example, you might have a 100x100 image you want to display on phones (screen size "normal"), you'd create image assets at 100x100, 150x150, 200x200 for drawable, drawable-hdpi, drawable-xhdpi folders respectively. But on 7" tablets, i.e. "large" screen size devices, you might display this same image at 200x200, so your "drawable-large" folder assets would be 200x200, 300x300, 400x400, and on 10" tablets, i.e. "xlarge" screens, you might display the same image at 300x300, 450x450, 600x600, so these go in "drawable-xlarge-*" folders.

All the details are here:

http://developer.android.com/guide/practices/screens_support.html




回答4:


First you need all the possible screen layouts

drawable
drawable-ldpi
drawable-mdpi
drawable-hdpi 
drawable-xhdpi   
drawable-xxhdpi // phones like s4  
drawable-xlarge
drawable-tvdpi  // nexus 7 etc 
drawable-xlarge-xhdpi //tablet like nexus 10  

layout : for phone 
layout-sw600dp
layout-sw720dp

then you need to put all use 9- patch images for buttons etc ... you can also make your custom drawable it would be easy and handy to work on ..Also you can take dpi for each screen by using switch and scale it the layout accordingly.

As, in one of my project I had used this technique for showing respective thing to each resolution device .

DisplayMetrics metrics = getResources().getDisplayMetrics();

int densityDpi = (int) metrics.density ;
switch (densityDpi) {
     case (int) 1.5: 
         break;

     case (int) 2: //1.75 will be 2 in INT.
         break; 

     default:
         break;
}

also keep all the values you are going to used for margin padding etc values-sw600dp for tvdpi tablet ,value-sw720dp for tablets

Last but not least keep all thing generic as much as you can and put it in drawable .. I have seen some ppl who used background patterns of different dpi's and put it in respective drawable .. if there is such thing like pattern make your custom drawble and repeat it accordingly that will save your time .. hope it may help you




回答5:


In order to stretch all the images to match phones you can specify the image size using the sdp size unit. This size unit is relative to the screen size so it can fulfill your requirement.



来源:https://stackoverflow.com/questions/20124079/supporting-multiple-screen-size-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!