support for different screen sizes in android

大憨熊 提交于 2019-11-26 18:40:37

问题


i want to my apps support different screen sizes. i add folders "layout-small and layout-large " in /res directory. but XMLs inside this folders aren't accessible in my activity.so i add all my XMLs in default layout and add this code

if((getResources().getConfiguration().screenLayout && 
      Configuration.SCREENLAYOUT_SIZE_SMALL) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
          setContentView(R.layout.main1);
    }else if((getResources().getConfiguration().screenLayout &&
                Configuration.SCREENLAYOUT_SIZE_LARGE) == Configuration.SCREENLAYOUT_SIZE_LARGE){
                     setContentView(R.layout.main2);
        }
        else
            setContentView(R.layout.main);

in my activity, but when my AVD skin is 1024*600 and hw.lcd.dencity is 160 (large) it didn't work.

any help?


回答1:


Size: small, normal, large
Density: ldpi, mdpi, hdpi,
nodpi(no auto-scale) Aspect ratio: long, notlong
Orientation: land

Usage:

res/layout/my_layout.xml            
res/layout-small/my_layout.xml      
res/layout-large/my_layout.xml      
res/layout-large-long/my_layout.xml      
res/layout-large-land/my_layout.xml     
res/drawable-ldpi/my_icon.png  
res/drawable-mdpi/dpi/my_icon.png  
res/drawable-hdpi/my_icon.png      
res/drawable-nodpi/composite.xml   

Restricting your app to specific screen sizes(via the AndroidManifest):

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
...
<supports-screens
          android:largeScreens="true"
          android:normalScreens="true"
          android:smallScreens="true"
          android:anyDensity="true" />
...
</manifest>

And for code level tweeking:

float scale = getContext().getResources().getDisplayMetrics().density;

And don't forget:

dpi    = 160; //At 160dpi
pixels = dips * (density / dpi)

Also refer support multiple screen in android




回答2:


Layout name must be same

layout-small \ main1.xml
layout-normal\ main1.xml
layout-large \ main1.xml

You dont need to handle this, android automatically decide which layout will be used

setContentView(R.layout.main1);



回答3:


Please look at this:

<supports-screens android:smallScreens="true" 
      android:normalScreens="true" 
      android:largeScreens="true"
      android:xlargeScreens="true"
      android:anyDensity="true" />

res/layout/my_layout.xml         // layout for normal screen size ("default")
res/layout-small/my_layout.xml       // layout for small screen size
res/layout-large/my_layout.xml       // layout for large screen size
res/layout-xlarge/my_layout.xml      // layout for extra large screen size

res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation

res/drawable-mdpi/my_icon.png        // bitmap for medium density
res/drawable-hdpi/my_icon.png        // bitmap for high density
res/drawable-xhdpi/my_icon.png       // bitmap for extra high density

more read so please see this link How to support different screen size in android




回答4:


Android automatically does this for you. For the different screen sizes, make different xml files, but give them the same name, for example main.xml and put the one for large in the folder /res/layout-large, for small in /res/layout-small, et cetera.

in the onCreate(), just put setContentView(R.layout.main)

For more information, consider reading this site from Android Developers.




回答5:


Android will automatically pick up the best resource for a given particular device.If no matching resource is available, the system uses the default resource and scales it up or down as needed to match the current screen size and density.

The "default" resources are those that are not tagged with a configuration qualifier.



来源:https://stackoverflow.com/questions/14215690/support-for-different-screen-sizes-in-android

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