`
zhangyf1987hb
  • 浏览: 80633 次
  • 性别: 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各机型的屏幕适配

    利用dimens来适配各种不同的Android机型,不会出现拉伸压缩现象。

    android 适配各种机型布局

    现在android手机的屏幕样式一大推,如果要让软件都能匹配多种屏幕,那确实是一件很蛋疼的事,那话不多说怎么才能让软件匹配多种屏幕,答案就是权重也就是android:layout_weight。

    android屏幕适配

    android 屏幕适配,UI设计不可不知的设计知识,适配所有Android机型。

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

    主要介绍了Android 图片显示与屏幕适配的问题的相关资料,Android的分辨率问题是每个Android 开发者头疼的问题,那么这里给大家介绍个万能办法,需要的朋友可以参考下

    android 屏幕适配

    android不同机型之间的适配问题,。

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

    Android二维码扫描,适配任何机型,实现各种形式的扫描框,带手电筒,带屏幕高亮且不熄屏。

    Android 手机屏幕适配解决办法

    Android的屏幕适配,即使得某一元素在Android不同尺寸、不同分辨率的手机上具备相同的显示效果,这个问题一直以来都是我们Android开发者不得不面对的问题。本文参考了很多前人的博客,并对这一问题做一个总结,力求...

    通过PX适配所有Android机型

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

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

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

    CameraAdapt:Android相机屏幕适配

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

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

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

    Android 屏幕截图

    Android中实现屏幕截图的方式,包含提醒客户权限获取后的全机型适配方案。

    Android屏幕点击录制工具

    1.屏幕点击录制,以及播放点击事件程序说明: 本程序分为放入到手机系统里面的程序,和在window环境下使用批处理脚本,结合的方式完成。...如您的机型在适配时有问题,可mail我(本人机型MX2,亲测,可正常稳定使用)。

    Android应用程序资源管理框架 PPT

    Android应用程序主要由代码和资源组成。资源主要就是指那些与UI相关的东西,例如UI布局、字符串和图片等。...了解Android应用程序资源管理框架,有助于我们更好地开发出能够适配多种机型的应用程序。

    详解如何使用image-set适配移动端高清屏图片

    现在手机机型繁杂,比如iphone的机型一般就是dpr=2的4.7寸屏幕机型与dpr=3的5.5寸plus(比如iphone6s与iphone6s plus),安卓的dpr就比较不可描述了,2.x,3.x这种有小数的dpr也是随处可见。 而对于不同dpr的机型呢...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    鉴于市场上用户的手机型号、种类、屏幕分辨率等参差不齐,传统方式根据主流系统分别开发相应的系统耗时又耗力,为了高效开发并节约开发项目成本,本文采用Android+HTML5相结合的方式进行移动端Web系统的设计研发工作...

    DPGeneratorUtils.java

    屏幕适配时使用的工具,简单实用,自带的内容和设定几乎可以满足大部分的手机机型,也可以自定义一些屏幕分辨率进行生成

    reactnative popup-dialog

    react native开发的demo 弹出对话框,非常适用。适配机型屏幕大小

Global site tag (gtag.js) - Google Analytics