Android官方入門文檔-支援不同的螢幕
Android官方入門文檔[10]支援不同的螢幕 Supporting Different Screens
支援不同的螢幕
This lesson teaches you to
1.Create Different Layouts
2.Create Different Bitmaps
You should also read
•Designing for Multiple Screens
•Providing Resources
•Iconography design guide
這節課教你
1.建立不同的布局
2.建立不同的位元影像
你也應該閱讀
•設計為多螢幕
•提供資源
•影像學設計指南
Android categorizes device screens using two general properties: size and density. You should expect that your app will be installed on devices with screens that range in both size and density. As such, you should include some alternative resources that optimize your app’s appearance for different screen sizes and densities.
•There are four generalized sizes: small, normal, large, xlarge
•And four generalized densities: low (ldpi), medium (mdpi), high (hdpi), extra high (xhdpi)
Android的分類使用兩個常規屬性裝置螢幕:大小和密度。你應該預料到你的應用程式將與螢幕,範圍在規模和密度的裝置安裝。因此,你應該包括最佳化您的應用程式的外觀不同的螢幕大小和密度的一些替代資源。
•有四種通用尺寸:small, normal, large, xlarge(小,正常,大,超大)
•與4廣義密度:低(ldpi),中(mdpi),高(hdpi),特高(xhdpi)
To declare different layouts and bitmaps you'd like to use for different screens, you must place these alternative resources in separate directories, similar to how you do for different language strings.
聲明不同的布局和位元影像,你想用不同的螢幕上,你必須將這些替代資源,在不同的目錄,類似於你如何做不同的語言字串。
Also be aware that the screens orientation (landscape or portrait) is considered a variation of screen size, so many apps should revise the layout to optimize the user experience in each orientation.
另外要注意,螢幕方向(橫向或縱向)被認為是螢幕尺寸的變化,因此許多應用程式應該修改的布局,以最佳化每個方向的使用者體驗。
Create Different Layouts
建立不同的布局
--------------------------------------------------------------------------------
To optimize your user experience on different screen sizes, you should create a unique layout XML file for each screen size you want to support. Each layout should be saved into the appropriate resources directory, named with a - suffix. For example, a unique layout for large screens should be saved under res/layout-large/.
為了最佳化在不同螢幕尺寸的使用者體驗,你應該建立為要支援的每個螢幕尺寸獨特的布局XML檔案。每個布局應儲存到相應的資來源目錄,命名了 - 尾碼。例如,對於大螢幕獨特的布局應儲存在res/layout-large/。
Note: Android automatically scales your layout in order to properly fit the screen. Thus, your layouts for different screen sizes don't need to worry about the absolute size of UI elements but instead focus on the layout structure that affects the user experience (such as the size or position of important views relative to sibling views).
註:Android的自動縮放你的布局,以妥善適應螢幕。因此,你的布局不同的螢幕尺寸不必擔心UI元素的絕對規模,而是著眼於布局結構,影響了使用者的體驗(如大小或重要意見位置相對於同級的意見)。
For example, this project includes a default layout and an alternative layout for large screens:
例如,該項目包括一個預設布局以及用於大螢幕的替代布局:
MyProject/
res/
layout/
main.xml
layout-large/
main.xml
The file names must be exactly the same, but their contents are different in order to provide an optimized UI for the corresponding screen size.
檔案名稱必須是完全一樣的,但它們的內容是為了提供用於相應的螢幕尺寸最佳化的使用者介面不同。
Simply reference the layout file in your app as usual:
像往常一樣,在您的應用程式中簡單的引用布局檔案:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
The system loads the layout file from the appropriate layout directory based on screen size of the device on which your app is running. More information about how Android selects the appropriate resource is available in the Providing Resources guide.
該系統從載入基於其上的應用程式正在啟動並執行裝置的螢幕大小適當布局的目錄布局檔案。關於Android如何選擇合適的資源的更多資訊,請在提供資源指南。
As another example, here's a project with an alternative layout for landscape orientation:
再舉一個例子,這裡是與橫向的替代布局的項目:
MyProject/
res/
layout/
main.xml
layout-land/
main.xml
By default, the layout/main.xml file is used for portrait orientation.
預設情況下,layout/main.xml中檔案用於縱向。
If you want to provide a special layout for landscape, including while on large screens, then you need to use both the large and land qualifier:
如果您想為景觀特殊的布局,包括同時在大螢幕上,那麼你需要同時使用large和land限定符:
MyProject/
res/
layout/ # default (portrait)
main.xml
layout-land/ # landscape
main.xml
layout-large/ # large (portrait)
main.xml
layout-large-land/ # large landscape
main.xml
Note: Android 3.2 and above supports an advanced method of defining screen sizes that allows you to specify resources for screen sizes based on the minimum width and height in terms of density-independent pixels. This lesson does not cover this new technique. For more information, read Designing for Multiple Screens.
註:Android的3.2及以上支援定義螢幕尺寸的先進方法,它允許您根據密度無關像素方面的最小寬度和高度,以指定的螢幕大小的資源。這節課不包括這種新技術。欲瞭解更多資訊,請閱讀設計的多個螢幕。
Create Different Bitmaps
建立不同的位元影像
--------------------------------------------------------------------------------
You should always provide bitmap resources that are properly scaled to each of the generalized density buckets: low, medium, high and extra-high density. This helps you achieve good graphical quality and performance on all screen densities.
你應該總是提供適當縮放到每個廣義密度桶位元影像資源:低,中,高,超高密度。這將協助你實現良好的圖形品質和效能上的所有的螢幕密度。
To generate these images, you should start with your raw resource in vector format and generate the images for each density using the following size scale:
•xhdpi: 2.0
•hdpi: 1.5
•mdpi: 1.0 (baseline)
•ldpi: 0.75
產生這些映像,你應該從你的向量格式的原始資源,產生了使用以下尺寸的規模每次密度映像:
•xhdpi:2.0
•hdpi:1.5
•mdpi:1.0(基準)
•ldpi:0.75
This means that if you generate a 200x200 image for xhdpi devices, you should generate the same resource in 150x150 for hdpi, 100x100 for mdpi, and 75x75 for ldpi devices.
這意味著,如果你產生一個200x200的映像xhdpi裝置,你應該產生ldpi裝置同一資源在150×150的hdpi,100×100的mdpi和75x75。
Then, place the files in the appropriate drawable resource directory:
然後,將在適當的繪製資來源目錄中的檔案:
MyProject/
res/
drawable-xhdpi/
awesomeimage.png
drawable-hdpi/
awesomeimage.png
drawable-mdpi/
awesomeimage.png
drawable-ldpi/
awesomeimage.png
Any time you reference @drawable/awesomeimage, the system selects the appropriate bitmap based on the screen's density.
您引用@drawable/ awesomeimage任何時候,系統會選擇基於螢幕的密度適當的位元影像。
Note: Low-density (ldpi) resources aren’t always necessary. When you provide hdpi assets, the system scales them down by one half to properly fit ldpi screens.
注意:低密度(ldpi)資源並非總是必要的。當您向hdpi資源,該系統由一個半到正確合適ldpi螢幕縮放下來。
For more tips and guidelines about creating icon assets for your app, see the Iconography design guide.
對於更多的技巧和有關您的應用程式建立的表徵圖資產的指引,看到影像學設計指南。