日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Supporting Multiple Screens 翻译 支持各种屏幕(上)

發布時間:2024/4/15 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Supporting Multiple Screens 翻译 支持各种屏幕(上) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Supporting Multiple Screens 支持各種各樣的屏幕尺寸、屏幕密度

Android runs on a variety?of devices that offer different screen sizes and densities. For applications, the Android system provides a consistent development environment across devices and handles most of the work to adjust each application's user interface to the screen on which it is displayed. At the same time, the system provides APIs that allow you to control your application's UI for specific screen sizes and densities, in order to optimize your UI design for different screen configurations. For example, you might want a UI for tablets that's different from the UI for handsets.

Although the system performs scaling and resizing to make your application work on different screens, you should make the effort to optimize your application for different screen sizes and densities. In doing so, you maximize the user experience for all devices and your users believe that your application was actually designed for?theirdevices—rather than simply stretched to fit the screen on their devices.

By following the practices described in this document, you can create an application that displays properly and provides an optimized user experience on all supported screen configurations, using a single?.apkfile.

android運行在不同的屏幕尺寸和屏幕密度(譯者注:屏幕密度=(屏幕寬像素的平方+屏幕長像素的平方)開根號/屏幕對角線物理長度,其實屏幕密度可以理解為不同的屏幕材質)。對于應用程序來說,android系統提供相同的開發環境無關具體的手機并且自身處理大多數的不同的要顯示的應用程序用戶界面。同時,android系統提供APIs允許你去控制你的應用界面為了特殊屏幕尺寸和密度。

為了不同的屏幕配置有最優的UI設計。例如,你可以為了平板電腦和手機做不同的UI設計。

雖然系統會縮放應用來運行在不同的屏幕支持。但你應該為不同屏幕尺寸或不同的密度做不同設計。這樣做你可以使不同設備的用戶用戶體驗效果最大化,并且使他們相信你的應用就是為他們的手機而設計,這樣會比通過拉伸屏幕來適應他們的手機尺寸效果要好得多。

通過下面這些實踐描述,你可以為不同的手機創建顯示得當提供最佳用戶體驗的應用程序。

Note:?The information in this document assumes that your application is designed for Android 1.6 (API Level 4) or higher. If your application supports Android 1.5 or lower, please first readStrategies for Android 1.5.?

Also, be aware that?Android 3.2 has introduced new APIs?that allow you to more precisely control the layout resources your application uses for different screen sizes. These new features are especially important if you're developing an application that's optimized for tablets. For details, see the section about?Declaring Tablet Layouts for Android 3.2.

注意:這個文檔是為android1.6或更高設計而寫,如果你的應用支持1.5或更低,請先看Strategies for Android 1.5.?

同時注意android3.2引進的新的APIs允許你更準確的控制不同屏幕尺寸的布局。如果你正開發一個平板應用,這些新的特性是特別重要的。

?

Overview of Screens Support

屏幕支持總覽

?


This section provides an overview of Android's support for multiple screens, including: an introduction to the terms and concepts used in this document and in the API, a summary of the screen configurations that the system supports, and an overview of the API and underlying screen-compatibility features.

這章節提供了多屏幕支持的總覽,包括在文檔和API中提到的術語和概念,以及系統支持的屏幕配置匯總,還包括API總覽和根本屏幕兼容性。

Terms and concepts

術語和概念

Screen size屏幕尺寸Actual physical size, measured as the screen's diagonal.

For simplicity, Android groups all actual screen sizes into four generalized sizes: small, normal, large, and extra large.

實際的物理尺寸,也就是屏幕的對角線尺寸。

為簡單起見,Android把所有的實際此存分成四個普遍的組:小,正常,大,巨大。

Screen density屏幕密度The quantity of pixels within a physical area of the screen; usually referred to as dpi (dots per inch). For example, a "low" density screen has fewer pixels within a given physical area, compared to a "normal" or "high" density screen.

?

For simplicity, Android groups all actual screen densities into four generalized densities: low, medium, high, and extra high.

屏幕一個物理區域上像素點的總數,通常被稱作dpi(每英寸的像素點數)。例如,一個低密度屏幕比正常密度和高密度屏幕在給定的物理屏幕區域上的像素點更少。

為簡單起見,Android吧所有的屏幕密度分成四個普遍的組:低,中,高,超高。

Orientation方向The orientation of the screen from the user's point of view. This is either landscape or portrait, meaning that the screen's aspect ratio is either wide or tall, respectively. Be aware that not only do different devices operate in different orientations by default, but the orientation can change at runtime when the user rotates the device.屏幕方向是從用戶的視角來說,橫屏屏幕寬高比是寬的,豎屏意味著屏幕寬高比分別是高的,要知道不僅不同設備的默認的操作方向不同,而且在運行中當用戶旋轉設備時方向可以改變。Resolution分辨率The total number of physical pixels on a screen. When adding support for multiple screens, applications do not work directly with resolution; applications should be concerned only with screen size and density, as specified by the generalized size and density groups.一個屏幕的總的物理像素點。當為多屏幕提供支持時,應用程序不直接用像素工作;我們只應該關心應用程序運行設備的屏幕尺寸和密度,然后指定是哪個組的尺寸和哪個組的密度。Density-independent pixel (dp)邏輯密度單位A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.

The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple:?px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.

當你設計UI界面的時候你應該用虛擬的像素單位來表示布局的尺寸和位置。當屏幕密度是160dpi是這個邏輯密度單位等于一個物理像素,同時160dpi作為基線密度被系統認為是中等密度屏幕,當必要時系統在運行時顯示的處理基于當前屏幕的實際密度的dp單位的任何縮放,dp單位和屏幕像素轉換公式是px=dp*(dpi/160)。例如在一個240dpi的屏幕,1dp等于1.5像素,當你設計你應用程序ui時你應該總是用dp單位來處理不同的屏幕密度。

Range of screens supported

屏幕支持的范圍

Starting with Android 1.6 (API Level 4), Android provides support for multiple screen sizes and densities, reflecting the many different screen configurations that a device may have. You can use features of the Android system to optimize your application's user interface for each screen configuration and ensure that your application not only renders properly, but provides the best user experience possible on each screen.

只從Android1.6開始,Android支持多屏幕尺寸和密度,因為硬件設備可以有許多不同的屏幕尺寸。你能用Android系統的特性為每個不同的屏幕配置來優化你應用程序的用戶界面并且確保你的應用程序不僅可以恰當的顯示,而且還提供更好的用戶體驗。

To simplify the way that you design your user interfaces for multiple screens, Android divides the range of actual screen sizes and densities into:

  • A set of four generalized?sizes:?small,?normal,?large, and?xlarge

    Note:?Beginning with Android 3.2 (API level 13), these size groups are deprecated in favor of a new technique for managing screen sizes based on the available screen width. If you're developing for Android 3.2 and greater, see?Declaring Tablet Layouts for Android 3.2?for more information.

  • A set of four generalized?densities:?ldpi?(low),?mdpi?(medium),?hdpi?(high), and?xhdpi?(extra high)

簡單的方式:你為多屏幕設計界面,android將實際的屏幕尺寸和密度范圍為:

4個廣義的尺寸:小,中,大,巨大

注意:從Android3.2開始,這些廣義的尺寸分組被新的基于屏幕可以用寬的屏幕管理技術所取代,如果你正開發Android3.2或者更高版本,看Declaring Tablet Layouts for Android 3.2?。

4個廣義的密度:低,中,高,超高

The generalized sizes and densities are arranged around a baseline configuration that is a?normal?size and?mdpi(medium) density. This baseline is based upon the screen configuration for the first Android-powered device, the T-Mobile G1, which has an HVGA screen (until Android 1.6, this was the only screen configuration that Android supported).

Each generalized size and density spans a range of actual screen sizes and densities. For example, two devices that both report a screen size of?normal?might have actual screen sizes and aspect ratios that are slightly different when measured by hand. Similarly, two devices that report a screen density of?hdpi?might have real pixel densities that are slightly different. Android makes these differences abstract to applications, so you can provide UI designed for the generalized sizes and densities and let the system handle any final adjustments as necessary. Figure 1 illustrates how different sizes and densities are roughly categorized into the different size and density groups.

廣義的尺寸和密度分布在基礎配置周圍,基礎配置是指中型屏幕和中型密度。這個基礎是以第一個支持Android的硬件設備的T-Mobile G1手機為標準,G1手機有HVGA屏幕(android1.6之前,只支持這種屏幕配置)。

每個廣義的尺寸和密度是一個實際屏幕尺寸和密度的范圍,例如兩個同樣是中級尺寸的手機可能在手動測量是屏幕尺寸和屏幕寬高比會有細微的差別。同樣兩個同樣是高級密度的的手機真正的像素密度可能也會有細微差別。Android讓這些差別對于應用程序來說無關,所以你可以為廣義的屏幕尺寸和密度提供UI設計,如果必要讓系統來處理任何最后的調整。圖1粗略的表明如何按照不同尺寸密度分類成不同的廣義的尺寸和廣義的密度。

Figure 1.?Illustration of how Android roughly maps actual sizes and densities to generalized sizes and densities (figures are not exact).

As you design your UI for different screen sizes, you'll discover that each design requires a minimum amount of space. So, each generalized screen size above has an associated minimum resolution that's defined by the system. These minimum sizes are in "dp" units—the same units you should use when defining your layouts—which allows the system to avoid worrying about changes in screen density.

圖1粗略的表明如何按照不同尺寸密度分類成不同的廣義的尺寸和廣義的密度(圖標不準確)。

當你為不同屏幕尺寸設計你UI時,你會發現每個設計需要一個最低限度的空間。所以,系統定義每個廣義的屏幕尺寸都有最小分辨率。這些最小尺寸用dp單位-當你定義你的布局時你應該用相同的單位,這樣你的布局不必擔心屏幕密度的變化。

  • xlarge?screens are at least 960dp x 720dp
  • large?screens are at least 640dp x 480dp
  • normal?screens are at least 470dp x 320dp
  • small?screens are at least 426dp x 320dp

Note:?These minimum screen sizes were not as well defined prior to Android 3.0, so you may encounter some devices that are mis-classified between normal and large. These are also based on the physical resolution of the screen, so may vary across devices—for example a 1024x720 tablet with a system bar actually has a bit less space available to the application due to it being used by the system bar.

注意:這些最小屏幕尺寸在android3.0之前沒有很好的定義(譯者注:此處翻譯的未必準確),所以你可能遇到一些設備被歸類為既是中屏幕又是大屏幕。這些雖然是基于屏幕的物理分辨率,但是在不同的設備上會有許多變化,例如 1024*720的平板會帶一個系統欄,事實上對于應用程序來說可用空間會少一些因為這部分空間應被系統欄占用。

To optimize your application's UI for the different screen sizes and densities, you can provide?alternative resources?for any of the generalized sizes and densities. Typically, you should provide alternative layouts for some of the different screen sizes and alternative bitmap images for different screen densities. At runtime, the system uses the appropriate resources for your application, based on the generalized size or density of the current device screen.

為了為不同的屏幕尺寸和密度優化你的應用程序界面,你可以提供多個資源為任何廣義的尺寸和密度。特別是你應該為一些不同的屏幕尺寸提供多套布局并且為不同的屏幕密度提供多種?bitmap圖片。在運行時,系統基于當前設備的廣義尺寸和密度為你的應用程序提供適當的資源。

?

You do not need to provide alternative resources for every combination of screen size and density. The system provides robust compatibility features that can handle most of the work of rendering your application on any device screen, provided that you've implemented your UI using techniques that allow it to gracefully resize (as described in the?Best Practices, below).

你不需要為每個屏幕尺寸和密度的組合提供多個資源。系統提供健壯的兼容特性可以處理在不同設備上展示你應用程序的大部分的工作,如果你已經用技巧實現了UI,系統會優雅的調整布局的大小。

Note:?The characteristics that define a device's generalized screen size and density are independent from each other. For example, a WVGA high-density screen is considered a normal size screen because its physical size is about the same as the T-Mobile G1 (Android's first device and baseline screen configuration). On the other hand, a WVGA medium-density screen is considered a large size screen. Although it offers the same resolution (the same number of pixels), the WVGA medium-density screen has a lower screen density, meaning that each pixel is physically larger and, thus, the entire screen is larger than the baseline (normal size) screen.

注意:定義設備的廣義尺寸和密度特性時是相互獨立的,例如一個WVGA的高密度屏幕被定義為中級尺寸屏幕因為他的物理尺寸和G1(android的第一款手機,同時也是標準的屏幕配置)相同。另一方面,一個WVGA中密度屏幕是一個大屏幕手機。雖然它提供了相同的分辨率,但是WVGA中密度屏幕有更小的屏幕密度,這意味著每個像素物理上更大,因此這個屏幕就會比標準屏幕大。

Density independence

邏輯密度

Your application achieves "density independence" when it preserves the physical size (from the user's point of view) of user interface elements when displayed on screens with different densities.

當他保護你用戶界面元素的物理尺寸在不同密度下顯示時你的應用程序實現邏輯密度。

Maintaining density independence is important because, without it, a UI element (such as a button) appears physically larger on a low density screen and smaller on a high density screen. Such density-related size changes can cause problems in your application layout and usability. Figures 2 and 3 show the difference between an application when it does not provide density independence and when it does, respectively.

維護邏輯密度非常重要因為沒有它UI元素(如一個按鈕)低密度的屏幕上變大,在高密度的屏幕上變小,這些密度相關的尺寸變化可以導致你應用程序布局和可用性出現變化。圖2和圖3展示當一個應用程序沒有提供邏輯密度和提供邏輯密度的不同。

Figure 2.?Example application without support for different densities, as shown on low, medium, and high density screens.

Figure 3.?Example application with good support for different densities (it's density independent), as shown on low, medium, and high density screens.

The Android system helps your application achieve density independence in two ways:

  • The system scales dp units as appropriate for the current screen density
  • The system scales drawable resources to the appropriate size, based on the current screen density, if necessary

android系統幫助你的應用程序通過下面兩種方式實現邏輯密度:

系統會酌情縮放dp單位以適應當前的屏幕密度。

如果需要基于當前的屏幕密度系統縮放繪圖資源到適當的尺寸。

In figure 2, the text view and bitmap drawable have dimensions specified in pixels (px?units), so the views are physically larger on a low density screen and smaller on a high density screen. This is because although the actual screen sizes may be the same, the high density screen has more pixels per inch (the same amount of pixels fit in a smaller area). In figure 3, the layout dimensions are specified in density-independent pixels (dpunits). Because the baseline for density-independent pixels is a medium-density screen, the device with a medium-density screen looks the same as it does in figure 2. For the low-density and high-density screens, however, the system scales the density-independent pixel values down and up, respectively, to fit the screen as appropriate.

圖2,文本框和bitmap圖片尺寸用像素設定,所以文本框在低密度上更大,在高密度更小,這個因為雖然實際屏幕尺寸可能相同但是高密度的屏幕每英寸有更高的像素。圖3布局尺寸用dp設定,所以對于邏輯密度來說中級屏幕是標準配置,所以有中級配置的屏幕看上去和圖2一樣。對于低密度和高密度的屏幕來說系統縮放邏輯密度來適應屏幕大小。

In most cases, you can ensure density independence in your application simply by specifying all layout dimension values in density-independent pixels (dp?units) or with?"wrap_content", as appropriate. The system then scales bitmap drawables as appropriate in order to display at the appropriate size, based on the appropriate scaling factor for the current screen's density.

However, bitmap scaling can result in blurry or pixelated bitmaps, which you might notice in the above screenshots. To avoid these artifacts, you should provide alternative bitmap resources for different densities. For example, you should provide higher-resolution bitmaps for high-density screens and the system will use those instead of resizing the bitmap designed for medium-density screens. The following section describes more about how to supply alternative resources for different screen configurations.

在大多數的情況下,你可以簡單的通過用dp來設置所有布局用的尺寸或酌情用wrap_content來確保你的應用程序密度獨立。系統酌情縮放圖片資源為了顯示適當的尺寸,基于當前屏幕密度進行適當的縮放因子。然而,位圖縮放可以作用于模糊的位圖和像素化的位圖,你可能注意的屏幕的界面。為了避免這些圖片的模糊,你可以提供多個位圖資源為不同的密度,例如你可以提供高分辨率位圖為高密度,系統將會用這些代替縮放為中密度而設計的位圖。接下來的章節描述更多的關于如何用多資源來適應不同的屏幕配置。

How to Support Multiple Screens

怎么支持多屏幕


The foundation of Android's support for multiple screens is its ability to manage the rendering of an application's layout and bitmap drawables in an appropriate way for the current screen configuration. The system handles most of the work to render your application properly on each screen configuration by scaling layouts to fit the screen size/density and scaling bitmap drawables for the screen density, as appropriate. To more gracefully handle different screen configurations, however, you should also:

Android支持多屏幕的基礎是他有能力管理布局和位圖在當前屏幕以適當的方式展示。系統處理大多數的工作,酌情的通過縮放布局來適應屏幕的尺寸或密度酌情通過縮放位圖來支持不同的屏幕密度,以這種方式把你的應用程序更好的顯示在不同的屏幕配置上。為了更優雅的處理不同的屏幕配置你應該做如下:

  • Explicitly declare in the manifest which screen sizes your application supports
  • 清晰的在mainfest文件中定義你支持的屏幕尺寸
  • By declaring which screen sizes your application supports, you can ensure that only devices with the screens you support can download your application. Declaring support for different screen sizes can also affect how the system draws your application on larger screens—specifically, whether your application runs in?screen compatibility mode.

    To declare the screen sizes your application supports, you should include the?<supports-screens>?element in your manifest file.

  • 通過定義你應用程序支持的屏幕尺寸,你可以確保只有你支持的屏幕設備才下載你的應用,定義支持不同屏幕尺寸也能影響你應用程序在巨屏上的如何呈現,尤其是你應用程序運行在屏幕兼容模式下。
  • 你應該在mainfest文件的?<supports-screens>元素中定義你支持的屏幕尺寸。
  • Provide different layouts for different screen sizes
  • 為不同的屏幕尺寸提供不同的布局

  • By default, Android resizes your application layout to fit the current device screen. In most cases, this works fine. In other cases, your UI might not look as good and might need adjustments for different screen sizes. For example, on a larger screen, you might want to adjust the position and size of some elements to take advantage of the additional screen space, or on a smaller screen, you might need to adjust sizes so that everything can fit on the screen.

    The configuration qualifiers you can use to provide size-specific resources are?small,?normal,?large, andxlarge. For example, layouts for an extra large screen should go in?layout-xlarge/.

    Beginning with Android 3.2 (API level 13), the above size groups are deprecated and you should instead use the?sw<N>dp?configuration qualifier to define the smallest available width required by your layout resources. For example, if your multi-pane tablet layout requires at least 600dp of screen width, you should place it inlayout-sw600dp/. Using the new techniques for declaring layout resources is discussed further in the section about?Declaring Tablet Layouts for Android 3.2.

  • 默認情況下android會重繪你的布局來適應設備屏幕,在大多數情況下,工作良好,在某些情況下,你的UI可能看上去不ok并且可能需要為不同的屏幕尺寸調整。例如一個巨屏你可能想利用額外的屏幕空間通過調整一些控件的位置和尺寸,或者在一些小屏幕上,你可能需要調整尺寸來顯示出所有的內容。 你能用配置修飾符small,?normal,?large, 和xlarge提供指定尺寸資源。例如為超大屏準備的布局應該在layout-xlarge/下。
  • 從Android3.2開始,反對用以上尺寸組應該用sw<>dp配置修飾符替代來定義最小的所需寬度通過你的布局文件,例如如果多窗體平板布局需要至少600dp屏幕寬度,你應該放在layout-sw600dp/下。在Declaring Tablet Layouts for Android 3.2.中會更多的討論用新技術來定義布局文件。
  • Provide different bitmap drawables for different screen densities
  • 提供不同的位圖為不同的屏幕密度

  • By default, Android scales your bitmap drawables (.png,?.jpg, and?.gif?files) and Nine-Patch drawables (.9.png?files) so that they render at the appropriate physical size on each device. For example, if your application provides bitmap drawables only for the baseline, medium screen density (mdpi), then the system scales them up when on a high-density screen, and scales them down when on a low-density screen. This scaling can cause artifacts in the bitmaps. To ensure your bitmaps look their best, you should include alternative versions at different resolutions for different screen densities.

    The configuration qualifiers you can use for density-specific resources are?ldpi?(low),?mdpi?(medium),?hdpi(high), and?xhdpi?(extra high). For example, bitmaps for high-density screens should go in?drawable-hdpi/.

  • 默認情況下,Android縮放你的位圖文件(.png,?.jpg, 和?.gif?文件)和九宮格圖片(.9.png文件)使得他們在每個設備上以是適當的尺寸顯示。例如你的應用程序提供只為標準尺寸中級密度的屏幕提供位圖,那么在高密度系統會放大位圖,在低密度系統會縮小位圖。這些縮放能導致圖片模糊,為了確保你的圖片效果最佳,你應該為不同的屏幕密度提供不同版本的資源文件。
  • 你能為制定密度用的配置符是?ldpi?(低),?mdpi?(中),?hdpi(高), 和?xhdpi?(超高).例如為高密度屏幕準備的圖片應該放在?drawable-hdpi/下。

The size and density configuration qualifiers correspond to the generalized sizes and densities described inRange of screens supported, above.

Note:?If you're not familiar with configuration qualifiers and how the system uses them to apply alternative resources, read?Providing Alternative Resources?for more information.

這個尺寸和密度配置符相當于廣義的尺寸和密度在Range of screens supported中有描述。

注意:如果你不熟悉配置符并且不知道系統如果用這些資源,請閱讀?Providing Alternative Resources?。

At runtime, the system ensures the best possible display on the current screen with the following procedure for any given resource:

在運行時,系統為每個資源用下面的過程來確保在當前屏幕上有更好的顯示效果:

  • The system uses the appropriate alternative resource

    Based on the size and density of the current screen, the system uses any size- and density-specific resource provided in your application. For example, if the device has a high-density screen and the application requests a drawable resource, the system looks for a drawable resource directory that best matches the device configuration. Depending on the other alternative resources available, a resource directory with thehdpi?qualifier (such as?drawable-hdpi/) might be the best match, so the system uses the drawable resource from this directory.

  • If no matching resource is available, the system uses the default resource and scales it up or down as needed to match the current screen size and density

    The "default" resources are those that are not tagged with a configuration qualifier. For example, the resources in?drawable/?are the default drawable resources. The system assumes that default resources are designed for the baseline screen size and density, which is a normal screen size and a medium density. As such, the system scales default density resources up for high-density screens and down for low-density screens, as appropriate.

    However, when the system is looking for a density-specific resource and does not find it in the density-specific directory, it won't always use the default resources. The system may instead use one of the other density-specific resources in order to provide better results when scaling. For example, when looking for a low-density resource and it is not available, the system prefers to scale-down the high-density version of the resource, because the system can easily scale a high-density resource down to low-density by a factor of 0.5, with fewer artifacts, compared to scaling a medium-density resource by a factor of 0.75.

  • 1.基于當前系統的尺寸和密度系統會選擇適當的資源,系統會用應用程序提供任何指定尺寸和指定密度的文件。例如如果設備有高密度并且應用程序需要圖片資源,系統會尋找和屏幕配置相匹配的資源文件。依賴其他可用資源,一個名字包括hdpi的資源文件夾(例如drawable-hdpi),可能會更好的匹配,所以系統會直接用這些文件夾。

    2.如果沒有匹配的資源可用,系統用默認的資源或者是根據當前屏幕配置進行縮放。默認的資源是沒有配置符的,例如drawable/?的資源是默認資源。系統假定默認的資源是為標準屏幕尺寸和密度設計,標準屏幕尺寸和密度是中級尺寸和中級密度。系統會縮放資源為不同的屏幕配置。

    然而,當系系統在沒有找到指定密度的文件夾時,它不會總是用默認資源。當縮放時系統為了提供更好的結果,也許系統會使用其他的密度的資源。例如當尋找低密度的資源但沒找到時,系統更喜歡縮小高密度的資源,因為系統能輕易的按照0.5的比例縮小高密度資源為低密度資源,能輕易的按照0.75的比例縮小高密度資源為中密度資源。

    For more information about how Android selects alternative resources by matching configuration qualifiers to the device configuration, read?How Android Finds the Best-matching Resource.

    關于Android通過配置符來選擇資源匹配設備配置的更多內容請看?How Android Finds the Best-matching Resource.

    Using configuration qualifiers

    用配置符

    Android supports several configuration qualifiers that allow you to control how the system selects your alternative resources based on the characteristics of the current device screen. A configuration qualifier is a string that you can append to a resource directory in your Android project and specifies the configuration for which the resources inside are designed.

    To use a configuration qualifier:

    Android支持一些配置符,這些配置符通會讓系統根據不同的屏幕選擇不同的資源。配置符是一個字符串,這個字符串跟在資源文件夾名稱的后面,為預先設計的資源指定配置。

    用這些配置符可以做:

  • Create a new directory in your project's?res/?directory and name it using the format:<resources_name>-<qualifier>
    • <resources_name>?is the standard resource name (such as?drawable?or?layout).
    • <qualifier>?is a configuration qualifier from table 1, below, specifying the screen configuration for which these resources are to be used (such as?hdpi?or?xlarge).

    You can use more than one?<qualifier>?at a time—simply separate each qualifier with a dash.

  • Save the appropriate configuration-specific resources in this new directory. The resource files must be named exactly the same as the default resource files.
  • 1.在你的工程?res/?目錄下創建一個新的文件夾并且用<resources_name>-<qualifier>格式命名。

    <resources_name>是標準資源名字(如drawable?或?layout)

    <qualifier>是一個配置符從下面表1看,指定不同屏幕配置用的資源。

    For example,?xlarge?is a configuration qualifier for extra large screens. When you append this string to a resource directory name (such as?layout-xlarge), it indicates to the system that these resources are to be used on devices that have an extra large screen.

    ?例如,xlarge是巨屏的配置符,當你在資源文件夾的名字后面xlarge(例如layout-xlarge)時,它告訴系統這個資源文件是為巨屏的設備準備的。

    Table 1.?Configuration qualifiers that allow you to provide special resources for different screen configurations.

    Screen characteristic Qualifier Description

    SizesmallResources for?small?size screens.小屏幕
    normalResources for?normal?size screens. (This is the baseline size.)中屏幕,標準尺寸
    largeResources for?large?size screens.大屏幕
    xlargeResources for?extra large?size screens.巨屏幕
    DensityldpiResources for low-density (ldpi) screens (~120dpi).低密度
    mdpiResources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)中密度,標準密度
    hdpiResources for high-density (hdpi) screens (~240dpi).高密度
    xhdpiResources for extra high-density (xhdpi) screens (~320dpi).超高密度
    nodpi

    Resources for all densities. These are density-independent resources. The system does not scale resources tagged with this qualifier, regardless of the current screen's density.

    為所有的密度,無亂屏幕什么密度,系統都不縮放資源。

    tvdpi

    Resources for screens somewhere between mdpi and hdpi; approximately 213dpi. This is not considered a "primary" density group. It is mostly intended for televisions and most apps shouldn't need it—providing mdpi and hdpi resources is sufficient for most apps and the system will scale them as appropriate. If you find it necessary to provide tvdpi resources, you should size them at a factor of 1.33*mdpi. For example, a 100px x 100px image for mdpi screens should be 133px x 133px for tvdpi.

    這個資源是為在中密度和高密度質檢的屏幕準備的,近似于213dpi,這不認為是主要的密度分類。這幾乎是為電視準備,大多數的應用不需要他,mdpi和hdpi資源足夠給大多數的應用并且系統會酌情縮放他們。如果你發現必須要提供tvdpi資源,則你應該按照1.33*mdpi的比例來提供,例如中密度下100px *100px圖片,則tvdpi應該是133px*133px圖片。

    OrientationlandResources for screens in the landscape orientation (wide aspect ratio).橫屏
    portResources for screens in the portrait orientation (tall aspect ratio).豎屏
    Aspect ratiolong

    Resources for screens that have a significantly taller or wider aspect ratio (when in portrait or landscape orientation, respectively) than the baseline screen configuration.

    比標準屏幕寬高比明顯的高或者寬的這樣屏幕。

    notlong

    Resources for use screens that have an aspect ratio that is similar to the baseline screen configuration.

    和標準屏幕配置一樣的屏幕寬高比的屏幕。

    Note:?If you're developing your application for Android 3.2 and higher, see the section about?Declaring Tablet Layouts for Android 3.2?for information about new configuration qualifiers that you should use when declaring layout resources for specific screen sizes (instead of using the size qualifiers in table 1).

    注意:如果你為Android3.2或更高的應用開發,看Declaring Tablet Layouts for Android 3.2?章節關于當為特定屏幕開發你應該用的新的配置符。

    ?

    For more information about how these qualifiers roughly correspond to real screen sizes and densities, seeRange of Screens Supported, earlier in this document.

    關于這些配置符如何大致相當于真實的屏幕尺寸和密度,請看Range of Screens Supported,或更早的文檔。

    For example, the following is a list of resource directories in an application that provides different layout designs for different screen sizes and different bitmap drawables for medium, high, and extra high density screens.

    例如,下面的應用程序里資源文件夾列表為不同的屏幕尺寸提供了不同的布局,為不同的屏幕密度提供不同的圖片資源。

    res/layout/my_layout.xml ? ? ? ? ? ? // layout for normal screen size ("default") res/layout-small/my_layout.xml ? ? ? // layout for small screen size res/layout-large/my_layout.xml ? ? ? // layout for large screen size res/layout-xlarge/my_layout.xml ? ? ?// layout for extra large screen size res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientationres/drawable-mdpi/my_icon.png ? ? ? ?// bitmap for medium density res/drawable-hdpi/my_icon.png ? ? ? ?// bitmap for high density res/drawable-xhdpi/my_icon.png ? ? ? // bitmap for extra high density

    For more information about how to use alternative resources and a complete list of configuration qualifiers (not just for screen configurations), see?Providing Alternative Resources.

    Be aware that, when the Android system picks which resources to use at runtime, it uses certain logic to determing the "best matching" resources. That is, the qualifiers you use don't have to exactly match the current screen configuration in all cases in order for the system to use them. Specifically, when selecting resources based on the size qualifiers, the system will use resources designed for a screen smaller than the current screen if there are no resources that better match (for example, a large-size screen will use normal-size screen resources if necessary). However, if the only available resources are?larger?than the current screen, the system will not use them and your application will crash if no other resources match the device configuration (for example, if all layout resources are tagged with the?xlarge?qualifier, but the device is a normal-size screen). For more information about how the system selects resources, read?How Android Finds the Best-matching Resource.

    關于如何使用資源和配置符的完整清單,請看Providing Alternative Resources。

    值得注意的是,當Android系統運行時選擇哪個資源,它用某種邏輯來決定最佳匹配資源。也就是這個配置符不必在所有的情況完全匹配所有的屏幕尺寸。特別是當基于尺寸和密度選擇資源文件時,如果這沒有更佳的匹配系統會用比當前屏幕小的設計資源給當前屏幕(例如必要的話大屏幕會用中屏幕的資源)。然而如果唯一可用的資源比當前屏幕大,系統不會用他們并且如果沒有其他資源匹配設備時你的應用會崩潰(例如,如果所有的布局文件為巨屏設計,但是當前設備卻是中屏幕),更多信息關于系統如何選擇資源的,請讀How Android Finds the Best-matching Resource.

    Tip:?If you have some drawable resources that the system should never scale (perhaps because you perform some adjustments to the image yourself at runtime), you should place them in a directory with the?nodpiconfiguration qualifier. Resources with this qualifier are considered density-agnostic and the system will not scale them.

    ?提示:如果你有圖片資源系統系統永遠不縮放(也許因為在運行時你自己的做一些圖片調整),你應該把他們放在有nodpi為后綴的文件夾中。放到這個文件夾后就和系統的密度無關了并且系統也不會縮放他們了。

    Designing alternative layouts and drawables

    設計多布局和多圖片資源

    The types of alternative resources you should create depends on your application's needs. Usually, you should use the size and orientation qualifiers to provide alternative layout resources and use the density qualifiers to provide alternative bitmap drawable resources.

    你應該依照你應用程序的需要來創建資源的類型,通常,你應該用的尺寸標識符和方向標識符來提供布局資源,用密度標識符提供圖片資源。

    The following sections summarize how you might want to use the size and density qualifiers to provide alternative layouts and drawables, respectively.

    下面的章節總結了你可能為尺寸和密度提供的布局和圖片資源。

    Alternative layouts

    可選布局

    Generally, you'll know whether you need alternative layouts for different screen sizes once you test your application on different screen configurations. For example:

    • When testing on a small screen, you might discover that your layout doesn't quite fit on the screen. For example, a row of buttons might not fit within the width of the screen on a small screen device. In this case you should provide an alternative layout for small screens that adjusts the size or position of the buttons.
    • When testing on an extra large screen, you might realize that your layout doesn't make efficient use of the big screen and is obviously stretched to fill it. In this case, you should provide an alternative layout for extra large screens that provides a redesigned UI that is optimized for bigger screens such as tablets.

      Although your application should work fine without an alternative layout on big screens, it's quite important to users that your application looks as though it's designed specifically for their devices. If the UI is obviously stretched, users are more likely to be unsatisfied with the application experience.

    • And, when testing in the landscape orientation compared to the portrait orientation, you might notice that UI elements placed at the bottom of the screen for the portrait orientation should instead be on the right side of the screen in landscape orientation.

    通常,一旦你在不同的屏幕配置測試,你會知道是否你需要為不同的屏幕提供不同的布局。例如:

    當在一個小屏幕上測試,你可能發現你的布局不是完全的適應屏幕,例如一排按鈕可能在小屏幕的寬度上顯示不完全,在這種情況下你應該為小屏幕提供一個調整過的按鈕的尺寸和位置的可選的布局。

    當在巨屏上測試時,你可能意識到你的布局不能有效的利用大屏幕并且為了填充屏幕被明顯的拉伸了,在這種情況下,你應該為巨屏例如平板設備提供一個重新設計的更優的布局。

    雖然你的應用程序在沒有巨屏布局的情況下應該工作良好,但對于用戶來說非常重要的是看上去你的應用程序為他們的設備經過了特別的設計。如果UI被拉伸了,用戶可能更不滿意用戶體驗。

    并且,當在橫屏比較豎屏測試時,你可能會注意到為豎屏設計的被放置在屏幕底部的UI元素在橫屏情況下應該被放在右側。

    To summarize, you should be sure that your application layout:

    • Fits on small screens (so users can actually use your application)
    • Is optimized for bigger screens to take advantage of the additional screen space
    • Is optimized for both landscape and portrait orientations

    總結一下,你應該確保你的應用程序布局如下:

    適合小屏幕(實際上用戶能用你的應用程序)

    利用額外的屏幕空間為大屏幕做優化。

    支持橫屏和豎屏

    If your UI uses bitmaps that need to fit the size of a view even after the system scales the layout (such as the background image for a button), you should use?Nine-Patch?bitmap files. A Nine-Patch file is basically a PNG file in which you specific two-dimensional regions that are stretchable. When the system needs to scale the view in which the bitmap is used, the system stretches the Nine-Patch bitmap, but stretches only the specified regions. As such, you don't need to provide different drawables for different screen sizes, because the Nine-Patch bitmap can adjust to any size. You should, however, provide alternate versions of your Nine-Patch files for different screen densities.

    如果你的UI需要用位圖來填充一個控件的尺寸,甚至是在布局被縮放以后(例如按鈕的景背圖片)。你應該用九宮格圖片文件,一個九宮格圖片基本是上一個PNG文件,是一個你指定的兩維區域可以被拉伸的PNG圖片。當系統需要縮放帶有位圖的控件是,系統會拉伸九宮格圖片,但是只拉伸指定區域,這種情況下你不需要為不同的屏幕尺寸提供不同的圖片,所以九宮格圖片能使用任何尺寸。然而你應該為不同屏幕密度提供不同的圖片。

    Alternative drawables

    可選的圖片

    Figure 4.?Relative sizes for bitmap drawables that support each density.

    Almost every application should have alternative drawable resources for different screen densities, because almost every application has a launcher icon and that icon should look good on all screen densities. Likewise, if you include other bitmap drawables in your application (such as for menu icons or other graphics in your application), you should provide alternative versions or each one, for different densities.

    幾乎每個應用程序都應該為不同的屏幕密度提供不同的圖片資源,因為幾乎所有的應用都有一個桌面圖標并且這個圖標在所有的屏幕密度下都看著正常。同樣的如果在你的應用程序你包括其他圖片(例如為菜單圖標和其他的圖片)你應該為不同的密度提供不同版本的圖片。

    Note:?You only need to provide density-specific drawables for bitmap files (.png,?.jpg, or?.gif) and Nine-Path files (.9.png). If you use XML files to define shapes, colors, or other?drawable resources, you should put one copy in the default drawable directory (drawable/).

    注意:你只需要為位圖文件(.png,?.jpg, or?.gif)和(.9.png)提供指定密度資源。如果你用xml文件定義形狀,顏色,或其他資源文件,你應該拷貝一份在默認的資源文件夾中(drawable/)。

    To create alternative bitmap drawables for different densities, you should follow the?3:4:6:8 scaling ratio?between the four generalized densities. For example, if you have a bitmap drawable that's 48x48 pixels for medium-density screen (the size for a launcher icon), all the different sizes should be:

    • 36x36 for low-density
    • 48x48 for medium-density
    • 72x72 for high-density
    • 96x96 for extra high-density

    你應該按照3:4:6:8的比例為不同的密度創建不同的圖片資源,例如你有一個位圖是48*48像素的為中密度屏幕提供的(桌面圖標的尺寸),所有不同的尺寸應該是:

    • 36x36 低密度
    • 48x48 中密度
    • 72x72 高密度
    • 96x96 超高密度

    For more information about designing icons, see the?Icon Design Guidelines, which includes size information for various bitmap drawables, such as launcher icons, menu icons, status bar icons, tab icons, and more.

    關于設計圖標的信息請看Icon Design Guidelines,這有不同的圖片資源尺寸信息,例如桌面圖標,按鈕圖標,狀態爛圖標,tab圖標等。

    Declaring Tablet Layouts for Android 3.2

    為Android3.2定義布局


    For the first generation of tablets running Android 3.0, the proper way to declare tablet layouts was to put them in a directory with the?xlarge?configuration qualifier (for example,?res/layout-xlarge/). In order to accommodate other types of tablets and screen sizes—in particular, 7" tablets—Android 3.2 introduces a new way to specify resources for more discrete screen sizes. The new technique is based on the amount of space your layout needs (such as 600dp of width), rather than trying to make your layout fit the generalized size groups (such as?large?or?xlarge).

    The reason designing for 7" tablets is tricky when using the generalized size groups is that a 7" tablet is technically in the same group as a 5" handset (the?large?group). While these two devices are seemingly close to each other in size, the amount of space for an application's UI is significantly different, as is the style of user interaction. Thus, a 7" and 5" screen should not always use the same layout. To make it possible for you to provide different layouts for these two kinds of screens, Android now allows you to specify your layout resources based on the width and/or height that's actually available for your application's layout, specified in dp units.

    第一帶平板運行的是Android3.0系統,更好的定義平板布局的方式是把他們放到帶xlarge配置符的文件夾中(例如,res/layout-xlarge/)。為了適應其他平板類型和7寸的屏幕平板類型,Android3.2介紹了一個新的為更多屏幕尺寸指定資源的方法。新的技術是基于你布局需要的空間總數(例如600dp寬),而不是嘗試讓你的布局填充廣義的尺寸組(像大屏幕和巨屏)。

    設計7寸屏幕復雜的原因是當用廣義的尺寸組是技術上7寸平板和5寸的手機用相同的尺寸組(巨屏組)。同時這兩個設備在尺寸上比較接近,作為用戶互動的方式對于應用程序的UI所用的空間是明顯不同的。因此7寸和5寸屏幕應該用不同的布局,為了為這兩種屏幕提供不同的布局成為可能,Android現在允許你用dp單位指定你布局文件在屏幕上可用的的寬或高。

    For example, after you've designed the layout you want to use for tablet-style devices, you might determine that the layout stops working well when the screen is less than 600dp wide. This threshold thus becomes the minimum size that you require for your tablet layout. As such, you can now specify that these layout resources should be used only when there is at least 600dp of width available for your application's UI.

    例如,在你為平板設備提供設計布局后,你可決定在小于600dp的屏幕上停止使用這個布局。這個限制因此變成你平板布局的最小尺寸。在這種情況下,你能現在指定這些布局資源應該被用在至少是600dp寬的應用布局上。

    You should either pick a width and design to it as your minimum size, or test what is the smallest width your layout supports once it's complete.

    你應該選擇一個寬度設計作為你的最小尺寸,或者一旦設計完成測試你布局支持的最小寬度。

    Note:?Remember that all the figures used with these new size APIs are density-indpendent pixel (dp) values and your layout dimensions should also always be defined using dp units, because what you care about is the amount of screen space available after the system accounts for screen density (as opposed to using raw pixel resolution). For more information about density-indpendent pixels, read?Terms and concepts, earlier in this document.

    注意:記住,所有使用這些新的尺寸數據API是邏輯密度像素(dp)值并且你的布局尺寸也應該用用dp單位定義。因為你所關心的是計算屏幕密度后的可用空間(而不是使用原始像素分辨率)。更多關于邏輯密度的請看Terms and concepts,或更早的文檔。

    Using new size qualifiers

    用新的尺寸修飾符

    The different resource configurations that you can specify based on the space available for your layout are summarized in table 2. These new qualifiers offer you more control over the specific screen sizes your application supports, compared to the traditional screen size groups (small, normal, large, and xlarge).

    表2已經匯總了能按照可用空間的方式為不同布局

    你能基于可用空間的方式指定不同的資源配置應匯總在表2中,相比較傳統的屏幕尺寸組而然(小,中,大,巨大)這些新的標識符為你的應用程序支持特定的屏幕尺寸上提供更多的控制。

    Note:?The sizes that you specify using these qualifiers are?not the actual screen sizes. Rather, the sizes are for the width or height in dp units that are?available to your activity's window. The Android system might use some of the screen for system UI (such as the system bar at the bottom of the screen or the status bar at the top), so some of the screen might not be available for your layout. Thus, the sizes you declare should be specifically about the sizes needed by your activity—the system accounts for any space used by system UI when declaring how much space it provides for your layout. Also beware that the?Action Bar?is considered a part of your application's window space, although your layout does not declare it, so it reduces the space available for your layout and you must account for it in your design.

    注意:用這些標識符指定的尺寸不是實際的屏幕尺寸。相反,這用dp單位定義的寬或高對于你的界面是可用的。Android系統可能用一些系統UI(像在屏幕底部的系統欄或在頂部狀態欄),所以一些屏幕對于你布局可能不可用。因此你定義的尺寸應該被特指是你activity所需要的尺寸-系統計算系統的UI任何尺寸,當定義為你布局提供多少空間時。也當心action bar被認為是你應用程序界面的一部分,雖然你的布局沒有定義action bar。所以你應該為你的布局減去這部分空間并且在你的設計里應該計算它。

    Table 2.?New configuration qualifers for screen size (introduced in Android 3.2).

    表2為屏幕尺寸新的配置符(Android3.2引進的)

    ?

    Screen configuration Qualifier values Description

    smallestWidthsw<N>dp

    Examples:
    sw600dp
    sw720dp

    The fundamental size of a screen, as indicated by the shortest dimension of the available screen area. Specifically, the device's smallestWidth is the shortest of the screen's available height and width (you may also think of it as the "smallest possible width" for the screen). You can use this qualifier to ensure that, regardless of the screen's current orientation, your application's has at least?<N>?dps of width available for it UI.

    For example, if your layout requires that its smallest dimension of screen area be at least 600 dp at all times, then you can use this qualifer to create the layout resources,?res/layout-sw600dp/. The system will use these resources only when the smallest dimension of available screen is at least 600dp, regardless of whether the 600dp side is the user-perceived height or width. The smallestWidth is a fixed screen size characteristic of the device;the device's smallestWidth does not change when the screen's orientation changes.

    屏幕的基礎尺寸,就像可用屏幕區域的最短尺寸,尤其是設備的最小寬度是屏幕可用高度或寬度的最短尺寸(你可能會想它是屏幕最小可能寬度)。你能用這個標識符確保,無論屏幕的當前方向,你的應用程序寬度至少有<N>dps可用。例如,如果你的布局需要屏幕區域上的最小尺寸在任何時候至少是600dp,則你能用這個標識符創建布局資源res/layout-sw600dp/只有當屏幕的最小寬度是600dp時系統才會用這些資源文件,無論600dp是用戶認為的寬或高。最小屏幕寬度是固定的設備特有的屏幕尺寸;當屏幕的方向改變時設備的最小寬度不變。

    The smallestWidth of a device takes into account screen decorations and system UI. For example, if the device has some persistent UI elements on the screen that account for space along the axis of the smallestWidth, the system declares the smallestWidth to be smaller than the actual screen size, because those are screen pixels not available for your UI.

    設備的最小寬度要考慮屏幕的尺寸和系統UI,例如,如果在屏幕上設備有一些持久化UI元素沿著最小寬度軸線計算空間,系統聲明的最小寬度會比實際的屏幕尺寸小,因為這些屏幕像素對于UI來說不可用。

    This is an alternative to the generalized screen size qualifiers (small, normal, large, xlarge) that allows you to define a discrete number for the effective size available for your UI. Using smallestWidth to determine the general screen size is useful because width is often the driving factor in designing a layout. A UI will often scroll vertically, but have fairly hard constraints on the minimum space it needs horizontally. The available width is also the key factor in determining whether to use a one-pane layout for handsets or multi-pane layout for tablets. Thus, you likely care most about what the smallest possible width will be on each device.

    這個關于廣義的屏幕尺寸標識符(小,中,大,巨大)為有效的尺寸允許你定義一個連續的數字。用最小的寬度決定廣義屏幕尺寸是有用的因為寬度經常做除數在布局設計時。一個UI會經常垂直滾動,但是在水平方向上會適應的硬件約束需要的最小空間。可用寬度是決定為手機用一個布局關鍵因素,還是為平板用多個布局,因此,你關心每個設備上的最小寬度。

    Available screen widthw<N>dp

    Examples:
    w720dp
    w1024dp

    Specifies a minimum available width in dp units at which the resources should be used—defined by the?<N>?value. The system's corresponding value for the width changes when the screen's orientation switches between landscape and portrait to reflect the current actual width that's available for your UI.

    This is often useful to determine whether to use a multi-pane layout, because even on a tablet device, you often won't want the same multi-pane layout for portrait orientation as you do for landscape. Thus, you can use this to specify the minimum width required for the layout, instead of using both the screen size and orientation qualifiers together.

    Available screen heighth<N>dp

    Examples:
    h720dp
    h1024dp
    etc.

    Specifies a minimum screen height in dp units at which the resources should be used—defined by the?<N>?value. The system's corresponding value for the height changes when the screen's orientation switches between landscape and portrait to reflect the current actual height that's available for your UI.

    Using this to define the height required by your layout is useful in the same way as?w<N>dp?is for defining the required width, instead of using both the screen size and orientation qualifiers. However, most apps won't need this qualifier, considering that UIs often scroll vertically and are thus more flexible with how much height is available, whereas the width is more rigid.

    轉載于:https://www.cnblogs.com/xiaorenwu702/p/4231793.html

    總結

    以上是生活随笔為你收集整理的Supporting Multiple Screens 翻译 支持各种屏幕(上)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。