How to develop one Android application for different screens? [closed]

强颜欢笑 提交于 2019-12-21 02:38:22

问题


I want to develop an application for Galaxy Tab and Android phones. The tab and phone UI are very different. Like in tab, we have used fragments, etc. and on the phone we want to use tabs.

I am aware that by using layout-small or layout-large I can show different UIs, but what about functionality?

How should I proceed? Is there a simple way to handle functionality also for different screens? Or is it better to create two applications finally?


回答1:


First be sure to read this recent blog post that gives an overview of how applications see different screen sizes and can adjust to them:

http://android-developers.blogspot.com/2011/07/new-tools-for-managing-screen-sizes.html

This post has some examples of basic adjustments -- providing different layout files for different screen sizes.

This is also something that the fragment APIs are intended to help with. If you are already writing your UI with fragments, you are most of the way there -- showing these in tabs does not require a totally different implementation, just use the fragments to populate the different tabs.

If you are targeting Android 3.0 or later, this is easy because the action bar already has simple mechanisms to have its tabs allow switching between fragments. This API demo provides a very simple example of its use:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/ActionBarTabs.html

If you want to run on older versions of the platform, that is what the fragment APIs in the v4 support library are for. These allow you to write your app using fragments and still be able to run all the way down to Android 1.6 (API 4) of the platform. In MR2 we also added some sample code showing how you can use the support lib fragments with the old tab API.

Sample code for this is included in the now deprecated TabActivity class:

http://developer.android.com/reference/android/app/TabActivity.html




回答2:


There's no single answer to this - it's a broad topic.

One thing to keep in mind before you start, is that there's currently a lot of code floating around the web that assumes you can judge a screen size by the platform version: IE, everything up to Gingerbread is a phone, and Honeycomb (and up) is a tablet. This is a bad idea. Don't do this :) Also try to avoid a massive switch case that just executes different blocks of code based on width/height values you pull from the system. I'm not going to describe how to do that, others have already given that answer.

One thing to investigate is the Android Compatibility Package - It lets you use Fragments and Loaders (which were introduced in Honeycomb) in earlier versions of the Android platform. One common pattern this enables is to let you set up a nav fragment & content fragment which sit side-by-side on a Tablet (where there's room) but have the nav fragment switch over to the content fragment when an item is selected on a phone or smaller screen.

If you're just trying to have images that grow and shrink with screen size (that you're not drawing programmatically), the answer is 9-patches.

You also have the option, if it's appropriate for your application, of using Android Market's Multiple APK support. The linked page dives into how to set that up, and gives you a pretty solid description of when to use it and when you're better off with a single APK.




回答3:


You can retrieve the screen size using:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

You will have to have decided which screen sizes are required for the different features, such as setting the minimum tablet height and width to min_tab_height and min_tab_width, accordingly. Then do a check for if this screen size qualifies:

boolean is_tablet = false;
if (width >= min_tab_width && height >= min_tab_height) {
    is_tablet = true;
}

Now you can check if (is_tablet) before allowing tablet functionality to your app (or if (!is_tablet) for non-tablet functionality.




回答4:


Try splitting your app in seperate parts, one trunk containing core features that you develop as a Library, then two separate apps using the same core functionality with specific UI design, one for Tablet version of Android (>3.0 HoneyComb) and one for "phone" versions (<3.0).

My experience with GalaxyTab-likes (basically a tablet with a 'phone' non-HoneyComb version of the OS) is that phone layouts do stretch nicely to fit the 7 inch screen. So it might be enough to add a specialized layout.xml specifically made for big screen. However I didn't even found it necessary on my own app...

EDIT: also not that if a tablet is running a non-honeycomb OS, 2.2 or 2.3, like is the case with GalaxyTab and many 7 inch tablets, the functionality is exactly the same as on phones, so no specific redesign is needed if not for the layout of UI components.

EDIT: read this page for specific and detailed information on how to support different screen size in your app. You can specify in your manifest the size of the supported screen for your app with

<manifest ... >
    <supports-screens android:requiresSmallestWidthDp="600" />
    ...
</manifest>

So given the market now supports uploading several apk for the same app, you could compile different version of the app seperately for each screen size, and the market would filter them out according to the sizes in Manifest, so you should even never have to actually check for screen size in your code.




回答5:


//Determine screen size
if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {     
    Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();

}
else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {     
    Toast.makeText(this, "Normal sized screen" , Toast.LENGTH_LONG).show();

} 
else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {     
    Toast.makeText(this, "Small sized screen" , Toast.LENGTH_LONG).show();
}
else {
    Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();
}



回答6:


Similar to Phil's approach, if we are able to find the actual screen size we may be able to determine if the screen in big enough to support additional functions. Here is a thread discussing about this. LINK

android.os.Build file also seems to have some information.




回答7:


Use the android.os.Build.VERSION.SDK or the Build.VERSION.SDK_INT to get the actual OS at runtime. Put this in if or switch condition and put your logic inside the statement.



来源:https://stackoverflow.com/questions/7036369/how-to-develop-one-android-application-for-different-screens

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