简体   繁体   中英

Android App Screen Compatibility

I am currently working on a app and it might be used on all android devices. I find it really challenging to adjust my XML layout files according to various screen sizes. I have surfed a lot over this topic and found a useful doc at Developer site. The document is decent and provides enough information on what should be done for screen compatibility.

Questions :

1.If I have two different layouts in folders like res/layout-sw600dp and res/layout-sw720dp , will the app automatically decides which one of these layouts is to be used ?

2.Assuming that I prefer a ListView for handsets and GridView for Tabs as a Home Page display, how will I define my layouts and how will I refer them for UI ?

Any ideas on how I can pull off the 2nd question's feature will be highly appreciated. Thanks in advance.

You can defferenciate it in java by checking "hasHoneycomb", tabs will give tru as return value. One way is : you can set different layout from setcontentview() according to condition.

if(hasHoneycomb()) {
setcontentView(layout_for_tabs);
} else {
setcontentView(layout_for_phones);
}

Yes, app automatically decide which layout file to use for current device screen.

Give the ListView and GridView different ids. In code use findViewById() method when creating views. If ListView found ( findViewById(R.id.list) returned View) app is running on handset, otherwise ( findViewById(R.id.list) returned null and findViewById(R.id.grid) returned View) app is running on tablet.

1) Yes, it will, based on the screen size.

2) Give them different IDs and see which one is visible:

ListView mList = (ListView) findViewById( R.id.homeList );
GridView mGrid = (GridView) findViewById( R.id.homeGrid );

if( mList != null ) {
    // set list adapter
} else if( mGrid != null ) {
    // set grid adapter 
} else {
    // neither view exists...
}

For question 1, Yes Android will automatically choose and decide the most suitable amongst the two.

For 2, here is link - Determine if the device is a smartphone or tablet? Hope it helps. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM