android屏幕适配

屏幕适配一直是一个让人头疼的问题,论坛上讨论这个问题的最后也一般没有什么结果
有的提出使用RelativeLayout来进行相对位置的调整,也有的提出为每一个屏幕写出一套Layout来进行适配不同分辨率的屏幕。

我一直比较倾向使用RelativeLayout来进行编码,这样以后的代码维护量是比较少的,但是一些深度UI定制的程序使用RelativeLayout来进行的话也是行不通的,目前我们项目使用的Layout有上百个,所以没有办法全部使用RelativeLayout来进行处理,而且属于深度UI定制,所以只能通过制作不同的Values的dimens来进行处理了。
文件夹命名可以参考上边两个帖子,不过我还是将规则贴出来,便于大家参考。

优先级按照从上到下的方式查找。

Table 2. Configuration qualifiernames.
Qualifier Values Description
MCC and MNC Examples:
        mcc310
        mcc310-mnc004
        mcc208-mnc00
        etc.      
        The mobile country code (MCC), optionally followed by mobile network code (MNC)        from the SIM card in the device. For example, mcc310 is U.S. on any carrier,        mcc310-mnc004 is U.S. on Verizon, and mcc208-mnc00 is France on        Orange.
        If the device uses a radio connection (GSM phone), the MCC comes        from the SIM, and the MNC comes from the network to which the        device is connected.
        You can also use the MCC alone (for example, to include country-specific legalresources in your application). If you need to specify based on the language only, then use thelanguage and region qualifier instead (discussed next). If you decide to use the MCC andMNC qualifier, you should do so with care and test that it works as expected.
        Also see the configuration fields mcc, and mnc, which indicate the current mobile country codeand mobile network code, respectively.
      
Language and region Examples:
        en
        fr
        en-rUS
        fr-rFR
        fr-rCA
        etc.      
The language is defined by a two-letter ISO              639-1 language code, optionally followed by a two letter              ISO              3166-1-alpha-2 region code (preceded by lowercase "r").        
        The codes are not case-sensitive; the r prefix is used to        distinguish the region portion.        You cannot specify a region alone.
        This can change during the lifeof your application if the user changes his or her language in the system settings. See Handling Runtime Changes for information abouthow this can affect your application during runtime.
        See Localization for a complete guide to localizingyour application for other languages.
        Also see the locale configuration field, whichindicates the current locale.
      
Screen size         small
        normal
        large
        xlarge      
         
  • small: Screens based on the space available on a        low-density QVGA screen.  Considering a portrait HVGA display, this has        the same available width but less height—it is 3:4 vs. HVGA's        2:3 aspect ratio.  Examples are QVGA low density and VGA high        density.
  • normal: Screens based on the traditional        medium-density HVGA screen.  A screen is considered to be normal if it is        at least this size (independent of density) and not larger.  Examples        of such screens a WQVGA low density, HVGA medium density, WVGA        high density.
  • large: Screens based on the space available on a        medium-density VGA screen.  Such a screen has significantly more        available space in both width and height than an HVGA display.        Examples are VGA and WVGA medium density screens.
  • xlarge: Screens that are considerably larger than the traditional        medium-density HVGA screen. In most cases, devices with extra large screens would be toolarge to carry in a pocket and would most likely be tablet-style devices. Added in API Level9.
        Added in API Level 4.
        See Supporting MultipleScreens for more information.
        Also see the screenLayout configuration field,which indicates whether the screen is small, normal,or large.
      
Screen aspect         long
        notlong      
         
  • long: Long screens, such as WQVGA, WVGA, FWVGA
  • notlong: Not long screens, such as QVGA, HVGA, and VGA
Screen orientation         port
        land        
         
  • port: Device is in portrait orientation (vertical)
  • land: Device is in landscape orientation (horizontal)
Dock mode         car
        desk      
         
  • car: Device is in a car dock
  • desk: Device is in a desk dock
Night mode         night
        notnight      
         
  • night: Night time
  • notnight: Day time
        Added in API Level 8.
        This can change during the life of your application if night mode is left inauto mode (default), in which case the mode changes based on the time of day.  You can enableor disable this mode using UiModeManager. See Handling Runtime Changes for information about how this affects yourapplication during runtime.
      
Screen pixel density (dpi)         ldpi
        mdpi
        hdpi
        xhdpi
        nodpi      
         
  • ldpi: Low-density screens; approximately 120dpi.
  • mdpi: Medium-density (on traditional HVGA) screens; approximately160dpi.
  • hdpi: High-density screens; approximately 240dpi.
  • xhdpi: Extra high-density screens; approximately 320dpi. Added in APILevel 8
  • nodpi: This can be used for bitmap resources that you do not want to be scaledto match the device density.
        Added in API Level 4.
        There is thus a 3:4:6 scaling ratio between the three densities, so a 9x9 bitmap         in ldpi is 12x12 in mdpi and 18x18 in hdpi.
        When Android selects which resource files to use,         it handles screen density differently than the other qualifiers.         In step 1 of How Android finds the best         matching directory (below), screen density is always considered to         be a match. In step 4, if the qualifier being considered is screen         density, Android selects the best final match at that point,         without any need to move on to step 5.         
        See Supporting MultipleScreens for more information about how to handle screen sizes and how Android might scaleyour bitmaps.
      
Touchscreen type         notouch
        stylus
        finger      
         
  • notouch: Device does not have a touchscreen.
  • stylus: Device has a resistive touchscreen that's suited for use with astylus.
  • finger: Device has a touchscreen.
        Also see the touchscreen configuration field,which indicates the type of touchscreen on the device.
      
Keyboard availability         keysexposed
        keyssoft      
         
  • keysexposed: Device has a keyboard available. If the device has asoftware keyboard enabled (which is likely), this may be used even when the hardware keyboard isnot exposed to the user, even if the device has no hardware keyboard. If no softwarekeyboard is provided or it's disabled, then this is only used when a hardware keyboard isexposed.
  • keyshidden: Device has a hardware keyboard available but it ishidden and the device does not have a software keyboard enabled.
  • keyssoft: Device has a software keyboard enabled, whether it'svisible or not.
        If you provide keysexposed resources, but not keyssoftresources, the system uses the keysexposed resources regardless of whether akeyboard is visible, as long as the system has a software keyboard enabled.
        This can change during the life of your application if the user opens a hardwarekeyboard. See Handling Runtime Changes for information about howthis affects your application during runtime.
        Also see the configuration fields hardKeyboardHidden and keyboardHidden, which indicate the visibility of a hardwarekeyboard and and the visibility of any kind of keyboard (including software), respectively.
      
Primary text input method         nokeys
        qwerty
        12key      
         
  • nokeys: Device has no hardware keys for text input.
  • qwerty: Device has a hardware qwerty keyboard, whether it's visible to theuseror not.
  • 12key: Device has a hardware 12-key keyboard, whether it's visible to the useror not.
        Also see the keyboard configuration field,which indicates the primary text input method available.
      
Navigation key availability         navexposed
        navhidden      
         
  • navexposed: Navigation keys are available to the user.
  • navhidden: Navigation keys are not available (such as behind a closedlid).
        This can change during the life of your application if the user reveals the navigationkeys. See Handling Runtime Changes forinformation about how this affects your application during runtime.
        Also see the navigationHidden configurationfield, which indicates whether navigation keys are hidden.
      
Primary non-touch navigation method         nonav
        dpad
        trackball
        wheel      
         
  • nonav: Device has no navigation facility other than using thetouchscreen.
  • dpad: Device has a directional-pad (d-pad) for navigation.
  • trackball: Device has a trackball for navigation.
  • wheel: Device has a directional wheel(s) for navigation (uncommon).
        Also see the navigation configuration field,which indicates the type of navigation method available.
      
System Version (API Level) Examples:
        v3
        v4
        v7
        etc.
        The API Level supported by the device. For example, v1 for API Level1 (devices with Android 1.0 or higher) and v4 for API Level 4 (devices with Android1.6 or higher). See the Android API Levels document for more informationabout these values.
         
Caution: Android 1.5 and 1.6 only match resourceswith this qualifier when it exactly matches the system version. See the section below about  Known Issues for more information.
      



查找最匹配的资源。
How Android Finds the Best-matching ResourceWhen you request a resource for which you provide alternatives, Android selects whichalternative resource to use at runtime, depending on the current device configuration. Todemonstrate how Android selects an alternative resource, assume the following drawable directorieseach contain different versions of the same images:
drawable/drawable-en/drawable-fr-rCA/drawable-en-port/drawable-en-notouch-12key/drawable-port-ldpi/drawable-port-notouch-12key/And assume the following is the device configuration:

Locale = en-GB 
Screen orientation = port 
Screen pixel density = hdpi 
Touchscreen type = notouch 
Primary text input method = 12key

By comparing the device configuration to the available alternative resources, Android selectsdrawables from drawable-en-port. It arrives at this decision using the following logic:

Figure 2. Flowchart of how Android finds thebest-matching resource.



  • Eliminate resource files that contradict the device configuration.    The drawable-fr-rCA/ directory is eliminated, because itcontradicts the en-GB locale.
    drawable/drawable-en/drawable-fr-rCA/drawable-en-port/drawable-en-notouch-12key/drawable-port-ldpi/drawable-port-notouch-12key/
    Exception: Screen pixel density is the one qualifier that is noteliminated due to a contradiction. Even though the screen density of the device is hdpi,drawable-port-ldpi/ is not eliminated because every screen density isconsidered to be a match at this point. More information is available in the  Supporting MultipleScreens document.
  • Pick the (next) highest-precedence qualifier in the list (table 2).(Start with MCC, then move down.)
  • Do any of the resource directories include this qualifier?

    • If No, return to step 2 and look at the next qualifier. (In the example,  the answer is "no" until the language qualifier is reached.)
    • If Yes, continue to step 4.
  • Eliminate resource directories that do not include this qualifier. In the example, the systemeliminates all the directories that do not include a language qualifier:
    drawable/drawable-en/drawable-en-port/drawable-en-notouch-12key/drawable-port-ldpi/drawable-port-notouch-12key/
    Exception: If the qualifier in question is screen pixel density,Android selects the option that most closely matches the device screen density.In general, Android prefers scaling down a larger original image to scaling up a smalleroriginal image. See  Supporting MultipleScreens.
  • Go back and repeat steps 2, 3, and 4 until only one directory remains. In the example, screenorientation is the next qualifier for which there are any matches.So, resources that do not specify a screen orientation are eliminated:drawable-en/drawable-en-port/drawable-en-notouch-12key/The remaining directory is drawable-en-port.

Though this procedure is executed for each resource requested, the system further optimizessome aspects. One such optimization is that once the device configuration is known, it mighteliminate alternative resources that can never match. For example, if the configurationlanguage is English ("en"), then any resource directory that has a language qualifier set tosomething other than English is never included in the pool of resources checked (though aresource directory without the language qualifier is still included).

Note: The  precedence of the qualifier (in  table 2) is more importantthan the number of qualifiers that exactly match the device. For example, in step 4 above, the lastchoice on the list includes three qualifiers that exactly match the device (orientation, touchscreentype, and input method), while drawable-en has only one parameter that matches(language). However, language has a higher precedence than these other qualifiers, sodrawable-port-notouch-12key is out.

To learn more about how to use resources in your application, continue to Accessing Resources.




屏幕分辨率:1024x600
density:1(160)
文件夹:values-mdpi-1024x600

屏幕分辨率:1024x600
density:1.5(240)
文件夹:values-hdpi-683x400  由1024/1.5  600/1.5得到,需要四舍五入。

屏幕分辨率:800x480
density:1(160)
文件夹:values-mdpi-800x480

屏幕分辨率:800x480
density:1.5(240)
文件夹:values-hdpi-533x320  由800/1.5  480/1.5得到,需要四舍五入。

以此类推
一般情况下需要创建出values 、values-mdpi 、 values-hdpi文件夹,以备在一些没有规定的尺寸屏幕上找不到资源的情况。

然后在里面使用不同的dimens文件,Layout中不要使用显示的数字,所有的尺寸定义全都援引dimens里面的内容。

这样能够保证深度UI定制的情况

0105补充
******************************
在工程的default.properties中如果split.density=false,则分辨率适配的时候文件夹命名不需要与scale相除

屏幕分辨率:800x480
density:1.5(240)
文件夹:values-hdpi-800x480 
******************************

 

 

收藏自:http://www.eoeandroid.com/thread-53757-1-1.html

感谢:http://www.mangocd.com/

你可能感兴趣的:(Java,Android,移动开发,runtime,ui)