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

Android 机型适配之屏幕适配

阅读更多

Supporting Different Screens

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)

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 -<screen_size> suffix. For example, a unique layout for large screens should be saved underres/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).

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:

@Overrideprotectedvoid 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.

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.

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:

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.

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

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.

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.

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.

For more tips and guidelines about creating icon assets for your app, see the Iconography design guide.

分享到:
评论

相关推荐

    android 屏幕适配工具

    提供了android 屏幕适配方案,使用教程 https://blog.csdn.net/qq_17827919/article/details/81027491。

    android 适配各种机型布局

    标题“android 适配各种机型布局”和描述中提到的关键点在于“权重”(android:layout_weight),这是Android布局管理器(如LinearLayout)中的一个重要属性,用于解决屏幕适配问题。 权重属性主要应用于...

    Android各机型的屏幕适配

    在Android开发中,面对众多不同尺寸和分辨率的设备,如何进行有效的屏幕适配是一项至关重要的任务。本篇文章将深入探讨如何使用`dimens`资源来实现Android各机型的完美适配,避免界面元素因尺寸差异而出现拉伸或压缩...

    直接输入px自动适配所有android机型

    标题"直接输入px自动适配所有android机型"所描述的是一种解决策略,它允许开发者在布局文件(layout.xml)中直接使用像素(px)单位来定义UI元素,然后系统会自动处理适配工作,消除设备间的碎片化现象。 这个解决...

    适配android各种机型demo

    综上所述,这个"适配android各种机型demo"可能包含了如何利用以上方法进行屏幕适配的示例代码和实践,旨在简化开发过程,让开发者不必为每种机型编写单独的dimens资源文件。通过学习和应用这些技术,开发者可以创建...

    Android多屏幕适配及样式-设计开发指导书

    ### Android多屏幕适配及样式设计开发指导书 #### 一、引言 随着移动互联网技术的发展,Android设备种类繁多,不同的品牌、型号、屏幕尺寸以及分辨率等特性导致了多屏幕适配成为Android开发中的一项重要挑战。本文...

    Android 图片显示与屏幕适配的问题

    Android 图片显示与屏幕适配的问题解决方案 Android 图片显示与屏幕适配的问题是每个 Android 开发者头疼的问题,解决这个问题需要了解 Screen 的尺寸、密度、分辨率等多方面的知识。下面将详细介绍 Android 图片...

    自动生成Android适配不同机型、语言资源文件

    首先,对于Android的分辨率适配,Android系统采用了一种基于密度独立像素(Density Independent Pixel, dp)的机制来处理不同屏幕尺寸和分辨率的问题。开发者需要为不同的屏幕密度创建相应的资源文件夹,如`drawable...

    android不同机型的适配的解决方案之按比例伸缩篇

    总结起来,Android应用的按比例伸缩适配涉及屏幕属性理解、使用weight属性、百分比布局、自定义ViewGroup以及正确使用尺寸单位。通过这些技术,我们可以确保应用在各种设备上都能呈现出一致且美观的界面。持续关注...

    Android二维码扫描 可适配任何机型

    以下是关于"Android二维码扫描 可适配任何机型"的相关知识点: 1. **Android Camera API**: 扫描二维码的基础是调用Android的相机接口。早期版本的Android使用Camera API1,而较新的版本推荐使用Camera2 API,它...

    能适配各种机型的android二维码扫描

    在Android平台上,开发一款能适配各种机型的二维码扫描应用是一项技术挑战,因为Android设备的硬件配置和屏幕尺寸差异巨大。ZXing(Zebra Crossing)是Google开发的一个开源项目,提供了一套完整的二维码和条形码...

    Android 手机屏幕适配解决办法

    Android 手机屏幕适配是Android开发者面临的关键挑战之一,目标是确保应用界面在不同尺寸和分辨率的设备上保持一致的视觉效果。本文主要探讨如何有效地解决这一问题,包括理解基本概念、布局适配策略、图片适配以及...

    android开发 手机适配种类选择

    在Android应用开发中,手机适配是一个至关重要的环节,它涉及到应用能否在各种不同设备上顺畅运行和良好显示。本文将详细阐述如何针对不同的系统版本、分辨率和品牌进行适配,确保应用的广泛兼容性。 首先,我们...

    通过PX适配所有Android机型

    通过工具类生成dimen文件,给一个默认的分辨率即可,生成完成之后复制到res目录下面就可以使用,左右边距使用@dimen/x,上下使用@dimen/y,可以完美适配所有分辨率手机!

    android安卓开发app如何做到自适应手机屏幕大小,适应不同分辨率的手机.zip

    6. **适配屏幕方向**:应用应支持横屏和竖屏模式,并确保在两种模式下布局都能正确显示。在布局文件中,可以使用`screenOrientation`属性来指定特定布局的方向。 7. **使用比例和百分比布局**:Android提供了...

    android 主流分辨率及对应机型

    本文将详细介绍Android设备中的几种主流分辨率及其对应的典型机型,帮助开发者更好地进行适配工作。 #### 二、720×1280分辨率 720p分辨率(1280×720)是Android手机中较为常见的一种分辨率,其像素密度较高,...

    以px分辩率适配时的工具类

    以屏幕分辩率为基准为安卓各类设备及各个机型进行px适配。

    Android客户端机型适配解决方案.rar_文章/文档_Java_

    1. **屏幕适配**:Android提供了多种布局单位,如dp(密度无关像素)和sp(可缩放像素),帮助开发者创建适应不同屏幕尺寸的界面。此外,可以使用`dimen.xml`资源文件为不同屏幕尺寸提供不同尺寸的资源。同时,使用...

    CameraAdapt:Android相机屏幕适配

    Android相机屏幕适配 该项目主要是为了做相机下的屏幕适配 具体思路可以参见我的这篇文章 测试: 机型:华为荣耀8 (厂商:honor,型号:FRD-AL00) 相机支持的预览分辨率: 1080 x 1920 1080 x 1440 864 x 1536 960...

    Android 屏幕截图

    本文将详细讲解如何在Android系统中进行屏幕截图,并探讨获取权限后的全机型适配策略。 首先,Android系统提供了多种方式进行屏幕截图。最基础的方法是通过Java代码调用`android.view.View`类的`draw()`方法,将...

Global site tag (gtag.js) - Google Analytics