这节课程向你展示了通过如下方式支持不同的屏幕大小:

  • 确保你的布局能适当地调整大小来适应屏幕

  • 根据屏幕的配置提供适当的UI布局

  • 确保正确的布局被应用到正确的屏幕

  • 提供正确缩放的位图

使用"wrap_content"和“match_parent"

———————————————————————————————————————————————————————————

为了确保你的布局是灵活的,并且适应不同的屏幕大小,你应该使用”wrap_content"和“match_parent"设置一些视图组件的宽度和高度。如果你使用"wrap_content",这个视图的宽度或者高度设置为符合视图内部内容需要的最小尺寸。然而”match_parent"(在API8之前又名"fill_parent")使组件扩展来匹配他的父视图的尺寸。

通过使用“wrap_content"和"match_parent"尺寸值替代固定值尺寸,你的视图要么仅仅使用视图必要的空间,或者扩展来填充可用的空间。例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent"  android:id="@+id/linearLayout1"   android:gravity="center" android:layout_height="50dp"> <ImageView android:id="@+id/imageView1"  android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/logo" android:paddingRight="30dp" android:layout_gravity="left" android:layout_weight="0" /> <View android:layout_height="wrap_content"  android:id="@+id/view1" android:layout_width="wrap_content" android:layout_weight="1" /> <Button android:id="@+id/categorybutton" android:background="@drawable/button_bg" android:layout_height="match_parent" android:layout_weight="0" android:layout_width="120dp" style="@style/CategoryButtonStyle"/> </LinearLayout> <fragment android:id="@+id/headlines"  android:layout_height="fill_parent" android:name="com.example.android.newsreader.HeadlinesFragment" android:layout_width="match_parent" />
</LinearLayout> 

注意,实例使用”wrap_content"和“match_parent"指定组件的尺寸,而不是指定特定的大小。这允许布局正确的适配不同的屏幕大小和方向。

例如,这是这个布局在竖屏和横屏模式下的养子。注意组件的大小自动适配宽度和高度。

使用RelativeLayout

———————————————————————————————————————————————————————————

你可以使用嵌入LinearLayout实例和"wrap_content"和”match_parent"尺寸结合,构建相当复杂的布局。然而,LinearLayout不允许是精确的控制孩子视图的位置关系;在LinearLayout中的视图简单一行一行排列。如果你需要子视图有变化的朝向而不是一条直线,一个更好的解决办法是经常使用RelativeLayout,它允许你指定你的布局组件之间的空间关系。例如,你能对齐一个子视图在屏幕的左边,并且另一个视图在屏幕的右边。

例如:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/label" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Type here:"/> <EditText android:id="@+id/entry" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/label"/> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/entry" android:layout_alignParentRight="true" android:layout_marginLeft="10dp" android:text="OK" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/ok" android:layout_alignTop="@id/ok" android:text="Cancel" />
</RelativeLayout> 

图例展示了这个布局如何在一个QVGA屏幕中显示。

图例展示了在一个较大的屏幕中如何展现。

主要到尽管组件的大小被改变了,它们空间位置关系通过RelativeLayout.LayoutParams被指定保存。

使用大小限定符

——————————————————————————————————————————————————————————————

你能从一个灵活布局或者像前一个章节相对布局获得的仅仅由这些了。然而这些布局通过拉伸组件内部和外部空间,来适配不同的屏幕,它们可能不能为每个屏幕尺寸提供最好的用户体验。因此,你的应用程序不仅要实现灵活布局,但还应该提供几个不同的布局来匹配不同的屏幕配置。你也可以通过使用配置限定符,它允许在运行时基于当前设备的配置自动选择合适的资源(例如对不同的屏幕尺寸不同的布局设计)。

例如,一些应用程序针对大屏幕(这个应用在一个窗格中显示列表项目,和在另一个窗格中显示内容)实现了“两个窗格”模式。平板和电视屏幕对于同时填充两个窗格已经足够大,但是手机屏幕必须分开显示它们。所以,为了实现这些布局,你必须下面的文件:

  • res/layout/main.xml,单窗格(默认)布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/headlines" android:layout_height="fill_parent" android:name="com.example.android.newsreader.HeadlinesFragment" android:layout_width="match_parent" />
</LinearLayout>  

  • res/layout-large/main.xml,双窗格布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <fragment android:id="@+id/headlines" android:layout_height="fill_parent" android:name="com.example.android.newsreader.HeadlinesFragment" android:layout_width="400dp" android:layout_marginRight="10dp"/> <fragment android:id="@+id/article" android:layout_height="fill_parent" android:name="com.example.android.newsreader.ArticleFragment" android:layout_width="fill_parent" />
</LinearLayout> 

注意在第二个布局目录名称中的large限定符。这个布局将会在被分类为大屏幕的设备中使用(例如,7寸或者以上的平板)。另一个布局(没有限定符)将被在更小的设备中使用。

使用最小宽度限定符

——————————————————————————————————————————————————————————————

开发者的一个困难是在3.2之前的“大”屏幕设备,它通常包含戴尔的Streak,原来的Galaxy Tab,和7寸平板。然而,一些应用程序可能想针对这样分类(例如5寸和7寸设备)的不同设备显示不同布局,即使它们都被认为是“大”屏幕。这就是为什么Android在Android3.2中包含“最小宽度”限定符。

最小宽度限定符允许你把一个使用dp描述的,有明确最小宽度的屏幕作为目标。例如,典型的7寸平板有最少600dp宽度,所以如果你想你的UI在这些屏幕(但是在更小的屏幕仅仅有一个单独的列表)中有两个窗格,你可以使用和前面章节为单和双窗格布局相同的布局文件,但是使用sw600dp替代large尺寸限定符,来说明双窗格布局是用于最小宽度为600dp的屏幕。

  • res/layout/main.xml,单窗格(默认)布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/headlines" android:layout_height="fill_parent" android:name="com.example.android.newsreader.HeadlinesFragment" android:layout_width="match_parent" />
</LinearLayout>

  • res/layout-sw600dp/main.xml,双窗格布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <fragment android:id="@+id/headlines" android:layout_height="fill_parent" android:name="com.example.android.newsreader.HeadlinesFragment" android:layout_width="400dp" android:layout_marginRight="10dp"/> <fragment android:id="@+id/article" android:layout_height="fill_parent" android:name="com.example.android.newsreader.ArticleFragment" android:layout_width="fill_parent" />
</LinearLayout> 

这意味着最小宽度大于或者等于600dp的设备将会选择layout-sw600dp/main.xml(双窗格)布局,然而更小的屏幕将会选择layout/main.xml(单窗格)布局。

然而,在Android3.2之前的设备上这不能正常工作,因为它们不认识sw600dp作为大小限定符,所以你还是要使用larger限定符。因此,你应该有一个被命名为res/layout-large/main.xml的文件

使用布局别名

———————————————————————————————————————————————————————————

最小宽度限定符仅仅在Android3.2和更高的版本中可用。因此,你仍然应该使用抽象大小容器(small,normal,large和xlarge)来兼容更早的版本。例如,如果你设计你的UI,以便在手机上显示单窗格界面,但是在7寸平板,电视和其它的大屏幕上显示多窗格,你必须提供这些文件:

  • res/layout/main.xml:单窗格布局

  • res/layout-large/main.xml:多窗格布局

  • res/layout-sw600dp/main.xml:多窗格布局

最后两个文件时相同的,因为它们中的一个将匹配Android3.2的设备,另一个对于更早的Android版本的平板和电视更好。

为了避免平板和电视同样文件的重复(和造成让人头疼的维护),你可以使用别名文件。例如,你可以定义下面布局:

  • res/layout/main.xml,单面板布局

  • res/layout/main_twopanes.xml,双面板布局

并且添加两个文件:

  • res/values-large/layout.xml:

<resources> <item name="main" type="layout">@layout/main_twopanes</item>
</resources> 

  • res/values-sw600dp/layout.xml:

<resources> <item name="main" type="layout">@layout/main_twopanes</item>
</resources> 

后面两个文件具有相同的内容,但是它们实际上不会定义布局。它们只不过是设置main.xml为main_twopanes的一个别名。因为这些文件有large和sw600dp选择符,它们被应用到平板和电视上,不顾Android版本(3.2之前的平板和电视匹配large,3.2之后将会匹配sw600dp)。

使用方向限定符

———————————————————————————————————————

一些布局在横屏和竖屏方向都能显示的很好,但是它们的大部分都可以调整的更好。在新闻阅读器示例应用中,这里是在每个屏幕大小和方向布局的表现:

  • 小屏幕,竖屏:单窗格,图标

  • 小屏幕,横屏:单窗格,图标

  • 7寸平板,竖屏:单窗格,Action bar

  • 7寸平板,横屏:双窗格,宽,Action bar

  • 10寸平板,竖屏:双窗格,窄,Action bar

  • 10寸平板,横屏:双窗格,宽,Action bar

  • 电视,横屏:双窗格,宽,Action bar

那么这些布局都被定义在/res/layout/目录下的XML文件中。然把每一个布局分配给各种配置的屏幕,这个应用采用布局别名来匹配每个配置。

res/layout/onepane.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/headlines" android:layout_height="fill_parent" android:name="com.example.android.newsreader.HeadlinesFragment" android:layout_width="match_parent" />
</LinearLayout> 

res/layout/onepane_with_bar.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent"  android:id="@+id/linearLayout1"   android:gravity="center" android:layout_height="50dp"> <ImageView android:id="@+id/imageView1"  android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/logo" android:paddingRight="30dp" android:layout_gravity="left" android:layout_weight="0" /> <View android:layout_height="wrap_content"  android:id="@+id/view1" android:layout_width="wrap_content" android:layout_weight="1" /> <Button android:id="@+id/categorybutton" android:background="@drawable/button_bg" android:layout_height="match_parent" android:layout_weight="0" android:layout_width="120dp" style="@style/CategoryButtonStyle"/> </LinearLayout> <fragment android:id="@+id/headlines"  android:layout_height="fill_parent" android:name="com.example.android.newsreader.HeadlinesFragment" android:layout_width="match_parent" />
</LinearLayout> 

res/layout/twopanes.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <fragment android:id="@+id/headlines" android:layout_height="fill_parent" android:name="com.example.android.newsreader.HeadlinesFragment" android:layout_width="400dp" android:layout_marginRight="10dp"/> <fragment android:id="@+id/article" android:layout_height="fill_parent" android:name="com.example.android.newsreader.ArticleFragment" android:layout_width="fill_parent" />
</LinearLayout> 

res/layout/twopanes_narrow.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <fragment android:id="@+id/headlines" android:layout_height="fill_parent" android:name="com.example.android.newsreader.HeadlinesFragment" android:layout_width="200dp" android:layout_marginRight="10dp"/> <fragment android:id="@+id/article" android:layout_height="fill_parent" android:name="com.example.android.newsreader.ArticleFragment" android:layout_width="fill_parent" />
</LinearLayout> 

现在所有可能的布局都被定义了,使用限定符仅仅是正确的布局和每个配置的简单映射。你现在能使用别名技术实现它:

res/values/layouts.xml:

<resources> <item name="main_layout" type="layout">@layout/onepane_with_bar</item> <bool name="has_two_panes">false</bool>
</resources> 

res/values-sw600dp-land/layouts.xml:

<resources> <item name="main_layout" type="layout">@layout/onepane</item> <bool name="has_two_panes">false</bool>
</resources> 

res/values-large-land/layouts.xml:

<resources> <item name="main_layout" type="layout">@layout/twopanes</item> <bool name="has_two_panes">true</bool>
</resources> 

res/values-large-port/layouts.xml:

<resources> <item name="main_layout" type="layout">@layout/twopanes_narrow</item> <bool name="has_two_panes">true</bool>
</resources> 

使用可拉伸的位图

——————————————————————————————————————————————————————————————

适配不同屏幕尺寸通常意味着你的图片资源也必须能够适应不同的尺寸。例如,一个按钮背景必须适应它应用的任何按钮形状。

如果你在会改变大小的组件上使用简单的图片,你将会很快发现效果有点不太尽如人意,因为在运行时将会拉伸或者收缩你的图片。解决方案是使用可拉伸的位图,它专门格式的图片,指定了哪些区域能拉伸和哪些区域不能。

因此,当设计奖将被使用在变化大小的组件上的位图时,通常使用可拉伸图片。将位图转换为可拉伸图片,你从一个普通图片开始(图4.4倍放大清晰显示)。

图4.button.png

然后使用SDK的draw9path工具打开它(被放在tools/目录下),通过沿着左和上边界绘制像素点来标记可拉伸的区域。你也可以通过沿着右和下边界绘制像素标记容纳内容的区域,结果如图5.

图5.button.9.png

注意沿着边界的黑色像素。在上和左边界的说明图片的这个区域可以被伸缩,和右和下边界的说明内容将被放置。

同样,注意.9.png扩展名。你必须使用这个扩展名,因为这是Android框架相对普通的PNG图片,发现它是一个可伸缩图片的标志。

当你在一个组件使用它作为背景的时候(通过设置android:background="@drawable/button"),框架伸缩图片来正确的适应按钮的尺寸,如在图6中的各种大小。

图6.一个使用button.9.png可拉伸图片按钮的各种尺寸。

新技术,新未来!欢迎大家关注“1024工场”微信服务号,时刻关注我们的最新的技术讯息!(甭客气!尽情的扫描或者长按!)

Android Developers:支持不同的屏幕大小相关推荐

  1. 移动设备应用程序中支持多个屏幕大小和 DPI 值

    支持多个屏幕大小和 DPI 值的指导原则 要部署独立于平台的应用程序,应了解不同的输出设备.设备可以具有不同的屏幕大小或分辨率以及不同的 DPI 值或密度. Flex 工程师 Jason SJ 在他的 ...

  2. Android针对不同的手机屏幕大小设计图片资源与编码

    一些术语 Screen Size 屏幕尺寸: 实际的物理尺寸,以屏幕的对角线为准(包括通知栏?) 将所有的实际尺寸分为四个广义的尺寸:small(小),normal(正常),large(大),extr ...

  3. android大屏适配_Android屏幕大小适配问题解决

    严格来说,作为读者,你应该带着批判性质的眼光来看这篇文章,此文章依据本人对Android官方开发资料<Supporting Multiple Screens>的阅读.实践以及和开发人员的沟 ...

  4. Android中如何获取手机屏幕大小

    我们可以通过使用类DisplayMetrics来获取手机屏幕的分辨率大小.DisplayMetrics类是获取手机屏幕各种属性的关键类.下面通过例子来展示如何获取手机屏幕的分辨率. 在布局文件main ...

  5. Android给scrollView截图超过屏幕大小形成长图

    很多的时候,我们想要分享一个界面的所有内容,可是内容太多,超过了屏幕的大小,简单的截屏已经满足不了我们的需要,这时候我们就可以根据布局里scrollView的高度来截取图片. 代码如下: [java] ...

  6. Android官方提供的支持不同屏幕大小的全部方法

    2019独角兽企业重金招聘Python工程师标准>>> 本文将告诉你如何让你的应用程序支持各种不同屏幕大小,主要通过以下几种办法: 让你的布局能充分的自适应屏幕 根据屏幕的配置来加载 ...

  7. ym——Android怎样支持多种屏幕

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 原文链接:http://developer.android.com/guide/pra ...

  8. Android如何支持多种屏幕

    支持多屏 Android涉及各种各样的支持不同屏幕尺寸和密度的设备.对于应用程序,Android系统通过设备和句柄提供了统一的开发环境,大部分工作是校正每一个应用程序的用户界面到它显示的屏上.与此同时 ...

  9. ym——Android如何支持多种屏幕

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 原文链接:http://developer.android.com/guide/pra ...

最新文章

  1. 微服务“大门”如何选择?
  2. 学习js权威指南第五站 ---- 数组
  3. linux shell case语句
  4. mysql里的ibdata1文件
  5. Leetcode_No.66 Plus One
  6. memset 结构体内指针_SideTable结构
  7. 使用DPM 2010备份还原Exchange2010单个邮箱
  8. VxWorks6.6 pcPentium BSP 使用说明(二):创建启动盘
  9. mysql将大表定时转储_mysql数据库数据定时封装转储
  10. Linux定时器:无节拍机制tickless(CONFIG_NO_HZ)
  11. JVM的内存管理 Ⅰ
  12. 基本数据结构----顺序表
  13. RXJAVA之Subject
  14. (转)黑手安全网QQ工具箱第三版 黑手一周年纪念版!
  15. 【渝粤题库】广东开放大学 建筑设备 形成性考核
  16. PHP汉字转拼音函数类
  17. 微信小程序企业号注册
  18. redhat 7.4系统安装英伟达独立显卡驱动步骤
  19. 一篇文章带你快速弄清楚什么是终端
  20. android netd守护进程机制 --- netd分析

热门文章

  1. 网络嵌入算法-Network Embedding-LINE/LANE/M-NMF
  2. C语言在坐标轴上输出曲线,C语言图形输出习题
  3. 基于STM32设计智能家居控制系统(OneNet)_2022
  4. 冷链食品追溯迫在眉睫,爱码物联3步助力冷链溯源
  5. 重温与解析《最后生还者》的互动叙事精髓(上)
  6. 关于3D可视化的几个知识点
  7. 练习题记录:求解距离矩阵,首先生成一百个二维坐标点,计算任意两个坐标点的距离
  8. 哈巴狗可以分为哪几种?
  9. 沃尔玛Walmart EDI解决方案之812报文解读
  10. 九招教你完全了解液晶拼接屏