来自三星的官方文档,觉得有点用,转了http://developer.samsung.com/technical-doc/view.do;jsessionid=AQpZJ6HoYL96UHXOPYWzLgbeHB9CJHAwXxUBI_v2I89TFCqPIP7B!869970213?v=T000000126
This document contains information on how to support devices of different resolutions in one app.
In order to understand this document, you need to know Android application development related to Layout and Resource.
The Aspect ratio policy for smartphones and tablets applies.
In order to support multiple resolutions in one app, you must create a variable layout for the app or web.
When creating a layout, use match_parent or weight, instead of fixed dp values, so that resolution is less affected.
<LinearLayout
android:layout_width="math_parent"
android:layout_height="match_parent"
android:orientation="vertical">
...
</LinearLayout>
<LinearLayout
android:layout_width="math_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
...
</LinearLayout>
To support multiple resolutions, you may use multiple layout files, but using a single layout file makes file maintenance and management easier. When you need to modify part of the layout for different resolutions, it is good to use dimens.xml, which makes it easy to modify.
Save the below name/value in this file. You can refer to it in source code and other layout XML files.
<resources>
<dimen name="textview_height">25dp</dimen>
<dimen name="textview_width">150dp</dimen>
<dimen name="font_size">16sp</dimen>
</resources>
As below, you can use the name/value defined as dimen above in the application's source code.
Resources res = getResources();
float fontSize = res.getDimension(R.dimen.font_size);
As below, you can use the name/value defined as dimen, in the Layout XML file as well.
<TextView
android:layout_height="@dimen/textview_height"
android:layout_width="@dimen/textview_width"
android:textSize="@dimen/font_size"/>
You can use the DisplayMetrics class to get lots of information about the display within your code, and create a dynamic screen to support many different resolutions.
// Utilize the display information (density, resolution, etc.) that uses the DisplayMetrics class in WindowManager.
DisplayMetrics metrics = new DisplayMetrics();
((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(metrics);
//Calculate the width of display in pixels.
int DisplayWidth = metrics.widthPixels;
// Calculate the height of display in pixels.
int DisplayHeight = metrics.heightPixels;
When displaying an image, if you specify ScaleType (place the image in a specific area, place it in the center, etc.), instead of fixing image size or position, you can support many different types of devices.
ImageView mImageView = (ImageView)findViewById(R.id.imageView);
mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType=“centerCrop"
android:src="@drawable/icon" />
To register a multi-resolution app in Android Market, you must describe the values to support multiple resolutions in the AndroidManifest.xml file. Each value can be defined for the screen range that your app supports. Please note that certain devices might not be shown in Android Market, depending on the values.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=http://schemas.android.com/apk/res/android>
<supports-screens android:resizeable="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>
</manifest>
To support multiple resolutions in one app, you can define layout and resource folders for each resolution.
By defining the Layout folder under the Res folder, as below, you can define the layout for multiple or specific resolutions. Below are the rules for creating the Layout folder. You can define each value for the screen range that your app supports. Please note that certain devices might not be shown in Android Market, depending on the values.
If you need to apply a different layout to specific screen size as shown in the screenshot on the right, create a folder with width and resolution, following the rules mentioned above. Then simply insert the XML file in the folder.
If you need a different layout for an OS version, define the target version like the "v19" folder at the bottom. For multi-resolution to which the above rules do not apply, refer to the "layout-resolution" folder. For layouts that do not affect resolution, refer to the "layout" folder.
By defining the Drawable folder under the Res folder, as below, you can define image resources for multiple or specific resolutions. Below are the rules for creating a folder.
If you need to apply a different image resource to a specific screen size as shown in the screenshot on the right, create a folder with width and resolution, following the rules mentioned above. Then simply insert the image in the folder.
If you need a different image resource for an OS version, define the target version like the "v19" folder at the bottom. For multi-resolution to which the above rules do not apply, refer to the "drawable-resolution" folder. For images that do not affect resolution, refer to the "drawable" folder.
Below are the current qualifiers for device screen characteristics. They are used to select the right resource for the current screen for the Android platform.
Screen Characteristic | Qualifier | Description |
---|---|---|
Size | small, normal, large, xlarge | Screen size type |
Density | ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpi | Density type |
Orientation | land, port | Landscape or portrait mode |
Under the project res folder, you can divide resources for each screen characteristic with the combination of <resource_name>-<qualifier>.
e.g.) res/layout-small/my_layout.xml //layout for small screens
res/drawable-land-mdpi// mdpi drawable for mdpi landscape screens
res/drawable-port-mdpi// mdpi drawable for mdpi portrait screens
res/drawable-xhdpi// xhdpi drawable for xhdpi screens
Even if resources for each screen characteristic do not match exactly, the Android platform will find and apply the optimal replacement resource.
e.g.) If a big-screen device does not have layout-large/my_layout.xml but has,
layout/my_layout.xml or layout-small/my_layout.xml, use the layout.
Please note that replacement resources only refer to the res folder of a smaller screen. If a small-screen device has a layout only in res/layout-large, it will not be referred to, and an error will occur.
This is how the Android platform selects the optimal resource and layout.
dawable/
dawable-en/
dawable-fr-rCA/
dawable-en-port/
dawable-en-notouch-12key/
dawable-port-ldpi/
dawable-port-notouch-12key/
Locale = en-GB
Screen orientation = port
Screen pixel density = hdpi
Touchscreen type = notouch
Primary text input method = 12key
Below are the types of resolutions for Samsung tablets.
MKT Name | Size | Resolution | key |
---|---|---|---|
Galaxy Tab S 10.5 | 10.5” | WQXGA (xhdpi) | Recent/Home/Back (H/W key) |
Galaxy Tab S 8.4 | 8.4” | WQXGA (xhdpi) | Recent/Home/Back (H/W key) |
Galaxy Tab 4 8.0 | 8.0” | WXGA (mdpi) | Recent/Home/Back (H/W key) |
Galaxy Note Pro 12.2 | 12.2” | WQXGA (xhdpi) | Recent/Home/Back (H/W key) |
Galaxy Tab pro 8.4 | 8.4” | WQXGA (xhdpi) | Recent/Home/Back (H/W key) |
Galaxy Note 10.1 2014 Edition | 10.1” | WQXGA (xhdpi) | Menu/Home/Back (H/W key) |
Galaxy Note 8.0 | 8.0” | WXGA (tvdpi) | Menu/Home/Back (H/W key) |
Galaxy Tab 3 8.0 | 8.0” | WXGA (tvdpi) | Menu/Home/Back (H/W key) |
Galaxy Note 10.1 2012 Edition | 10.1” | WXGA (mdpi) | Navigation bar in Display (S/W key) |
Galaxy Tab 8.9 LTE | 8.9” | WXGA (mdpi) | Navigation bar in Display (S/W key) |
Galaxy Tab 7.7 | 7.7” | WXGA (mdpi) | Navigation bar in Display (S/W key) |
You must divide navigation keys into those created as S/W buttons on the screen and those as H/W buttons. Also, depending on the model, you need to create a layout for recent keys and menu keys separately.
smallestWidth (sw) and available screen height (h) are used. If you use smallestWidth, you can provide each resolution. In order to categorize devices with the S/W navigation bar, you must use the available screen height. Because the height of 46dp (for the height of the navigation bar) exists, if you make the available screen height bigger than 46dp (e.g. 800-46=754), the resource folder might not be referred to. Please remember this. Below are examples of creating resource folders for Samsung tablets.
Galaxy Note 10.1 2012 Edition | drawable-sw800dp-mdpi | S/W key |
---|---|---|
Galaxy Note 3 8.0 Galaxy Note 8.0 |
drawable-sw600dp-tvdpi | H/W key |
Galaxy Tab 4 8.0 | drawable-sw800dp-h1255dp-port-mdpi drawable-sw800dp-h775dp-land-mdpi |
H/W key |
Galaxy Note 10.1 2014 Edition Galaxy Note Pro 12.2 |
drawable-sw800dp-xhdpi | H/W key |
ViewConfiguration.get(context).hasPermanentMenuKey() is supported in the Android API 14 and later.
For more information, please refer to the below chapters.