Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

support for different screen sizes in android

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?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...