This lesson teaches you to

  1. Create Different Layouts
  2. Create Different Bitmaps

You should also read

  • Designing for MultipleScreens
  • Supporting MultipleScreens
  • Iconography design guide

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 placethese alternative resources in separate directories, similar to how you do for different languagestrings.

Also be aware that the screens orientation (landscape or portrait) is considered a variation ofscreen size, so many apps should revise the layout to optimize the user experience in eachorientation.

Create Different Layouts

To optimize your user experience on different screen sizes, you should create a unique layout XMLfile 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 under res/layout-large/.

Note: Android automatically scales your layout in order toproperly fit the screen. Thus, your layouts for different screen sizes don'tneed to worry about the absolute size of UI elements but instead focus on the layout structure thataffects the user experience (such as the size or position of important views relative to siblingviews).

For example, this project includes a default layout and an alternative layout for largescreens:

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 providean optimized UI for the corresponding screen size.

Simply reference the layout file in your app as usual:

@Overrideprotected void 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 theappropriate resource is available in the Providing Resourcesguide.

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, thenyou 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 onthe minimum width and height in terms of density-independent pixels. This lesson does not coverthis new technique. For more information, read Designing for MultipleScreens.

Create Different Bitmaps

You should always provide bitmap resources that are properly scaled to each of the generalizeddensity buckets: low, medium, high and extra-high density. This helps you achieve good graphicalquality 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. Whenyou provide hdpi assets, the system scales them down by one half to properly fit ldpiscreens.

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

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 网站加载页面(HTML+CSS+JS,简易版)
  2. 你真的知道为什么要使用void(0)代替undef
  3. 你真的了解数据在堆栈中的存储方式吗?
  4. JavaScript中判断对象是否属于Array类型
  5. Foxwell NT530 BMW评论:我要怎么更新它呢?
  6. css盒模型以及如何计算盒子的宽度
  7. Visual Studio 2019 C++使用log4cplus
  8. 深入了解typeof与instanceof的使用场景及
  9. 你真的知道typeof null的结果为什么是‘o
  10. 推荐!5款Linux常用桌面环境