`
crazier9527
  • 浏览: 1010777 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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/

分享到:
评论

相关推荐

    Android 屏幕适配工具

    "Android屏幕适配"是Android开发中的核心概念,它涉及到尺寸单位、布局权重、资源多版本、比例适配以及使用百分比布局等多种技术。 1. 尺寸单位: - dp (density-independent pixels):与设备像素密度无关的像素...

    Android屏幕适配资源

    本资源包"Android屏幕适配资源"专注于解决这一问题,提供了values资源,能够帮助开发者轻松实现不同屏幕尺寸的适配。接下来,我们将详细讨论Android屏幕适配的核心概念、方法以及如何使用此资源包。 一、屏幕适配的...

    Android屏幕适配:dimens自动生成工具以及使用方法,亲测好用

    现在,针对"Android屏幕适配:dimens自动生成工具",这个工具的主要功能是自动化生成多个`dimens.xml`文件,减少手动创建和维护的工作量。使用该工具,开发者只需输入基本的尺寸数据,工具会根据预设的屏幕密度配置...

    Android-今日头条屏幕适配方案终极版一个极低成本的Android屏幕适配方案

    【Android屏幕适配方案终极版】是Android开发者面临的一个重要课题,尤其是在当今各种设备尺寸繁多的情况下。今日头条作为一款广受欢迎的信息聚合应用,其屏幕适配策略无疑具有很高的参考价值。这个低成本的解决方案...

    android屏幕适配px工具类

    "android屏幕适配px工具类"就是为了解决这一问题而设计的,它可以帮助开发者更方便地处理像素(px)单位转换,确保应用在不同屏幕上的显示效果一致。 在Android系统中,屏幕适配的核心是理解并利用密度独立像素(dp...

    Android屏幕适配主流方案的对比和集成

    本文将详述几种主流的Android屏幕适配方案,并进行对比分析,帮助开发者选择最适合项目需求的方法。 一、密度无关像素(DP/SP) Android系统采用密度无关像素(DP)作为单位来设计界面,以确保在不同密度的屏幕上...

    Android-针对Android屏幕适配所整合的几种常用方案

    本篇文章将详细探讨几种常见的Android屏幕适配方案,帮助开发者解决不同设备间界面显示不一致的问题。 1. **使用比例单位(dp、sp)** Android提供了密度无关像素(dp)和可缩放像素(sp)作为尺寸单位。dp用于长度和...

    Android屏幕适配生成文件

    Android屏幕适配生成文件

    android 屏幕适配方案小工具(可执行jar包)

    android 屏幕适配方案小工具(可执行jar包)

    Android代码-今日头条屏幕适配方案终极版,一个极低成本的 Android 屏幕适配方案

    A low-cost Android screen adaptation solution (今日头条屏幕适配方案终极版,一个极低成本的 Android 屏幕适配方案). Overview Pixel 2 XL | 1440 x 2880 | 560dpi: Pixel XL | 1440 x 2560 | 560dpi: ...

    android屏幕适配方案

    以下是一些关键的Android屏幕适配策略和技术: 1. **使用比例和相对布局(Relative Layout)**: - 不直接使用绝对像素值,而是使用比例(dp/dip)来定义控件大小,这样在不同密度的屏幕上会自动调整。 - 相对...

    Android屏幕适配smallestWidth适配尺寸文件

    本篇文章将深入探讨Android屏幕适配中的smallestWidth维度,以及如何使用对应的尺寸文件来优化应用界面。 首先,了解"smallestWidth"的概念。smallestWidth是指设备屏幕在横屏或竖屏状态下可用来显示内容的最小宽度...

    android屏幕适配方法

    android屏幕适配方法

    Android屏幕适配工具类

    标题中的"Android屏幕适配工具类"正是这样一个解决方案,它能自动生成适配不同屏幕尺寸的dimens资源文件,从而简化开发工作。 首先,我们来看看Android屏幕适配的基本概念。Android系统通过dp(density-independent...

    android屏幕适配工具

    本篇将详细讲解"android屏幕适配工具"的相关知识点,以及如何利用提供的"autolayout.jar"和"res"资源进行操作。 首先,我们要理解Android的屏幕适配机制。Android系统通过资源目录(如values、layout等)下的不同...

    android屏幕适配demo

    "android屏幕适配demo"是一个实例,它展示了如何确保应用程序在不同设备上看起来一致并保持良好的用户体验。在这个demo中,主要关注的是视图大小的调整和布局管理,以实现跨设备的兼容性。 首先,Android提供了多种...

    android 屏幕适配(百分比适配)

    总的来说,百分比适配是Android屏幕适配的一种有效策略,它简化了适配过程,使得开发者可以专注于应用的功能而非设备差异。通过正确理解和运用百分比适配技术,可以显著提高应用的用户体验。在实际开发中,结合其他...

    Android 屏幕适配总结

    Android 屏幕适配思维导图,花了两天的时间总结出来的,看思维导图会以一个整体的印象,有利于快速理解Android 屏幕适配的问题。

    Android代码-一种非常好用的Android屏幕适配

    本项目"Android代码-一种非常好用的Android屏幕适配"提供了一种有效的解决方案,帮助开发者解决不同屏幕尺寸和像素密度下的显示问题。项目名称为"ScreenAdaptation",表明它专注于屏幕适应性策略,旨在提升应用在...

Global site tag (gtag.js) - Google Analytics