Android的屏幕类型有几百种不同的尺寸,从小型的手机到大型的电视机。因此要使我们的应用程序兼容不同屏幕尺寸,才可以让我们的应用提供给更多的用户使用。

一、支持不同的屏幕尺寸

1、使用“wrap_content"和”match_parent"

为了确保布局的灵活性,来适应不同尺寸的屏幕,我们应该使用“wrap_content"来匹配组件的最小尺寸和使用”match_parent"来设置某些视图来匹配父视图的大小。这样设置和直接设置视图大小(如48dip)不同的是该视图的空间可以随着屏幕尺寸(父视图的大小)随意扩展。例如如下布局:

<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>

当我们翻转手机屏幕可以看到横向和竖向的屏幕适配如下:

2、使用Relative_layout

我们可以使用LinearLayout通过“wrap_content"和”match_parent"来实现很多复杂的布局,但是LinearLayout擅长控制的是线性的关系,如果是要精确控制平面关系,使用Relative_layout比较合适,它可以指定两个组件之间的空间关系。例如下面代码:

<?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手机上显示如下:

在大屏幕的WSVGA屏幕上显示如下:

3、使用尺寸限定符

上面我们通过“wrap_content"、”match_parent"、“relative_layout"的方法来实现不同屏幕尺寸的视图伸展,有效的解决了屏幕尺寸问题,除了这些方法外我们还可以使用尺寸限定符来针对不同的屏幕来做特殊的界面布局。

第一列是屏幕的特征,有尺寸、密度、方向、长宽比

第二列是限定符的写法(如large,我们就需要建立一个文件夹名称为layout-large)

第三列是相关解释和描述

例如我们现在在我们的res下建立两个文件夹通过不同的限定符文件夹名称来匹配不同尺寸的屏幕

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>

上面布局中在大屏幕中我们将两个大视图放到了整个屏幕,二小屏幕中我们只放置了一个视图。我们的设备会根据屏幕尺寸来自动寻找自己的布局。

4、使用最小宽度限定符

上面根据屏幕尺寸来匹配不同的布局文件,但是很多时候我们可能在不同的大屏手机中需要分别显示不同的布局(例如Galaxy Tab显示5个还是7个),在Android3.2以后就有了最小宽度限定符来解决这个问题。

假设sw600dp来表示我们的最小宽度,则布局文件就可以有如下表示:

小屏幕手机适配的布局  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>

设备宽度大于或等于600dp的布局 res/layout/layout-sw600dp.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>

要注意的是这种限定符在3.2以前的版本手机上是不支持的。在3.2之前还必须用尺寸限定符large来代替。

5、使用layout别名

综合上面的分析,如果我们要支持一个包含大屏幕尺寸的设备必须包含例如下面几个布局来匹配

res/layout/main.xml

res/layout/layout-large

res/layout/layout-sw600dp

最后这两个文件的作用是相同的,是为了支持3.2以下和以上版本,为了避免混淆和维护难度,我们可以给这些布局起别名,比如下面的布局名:

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和mian_twospanes的联系,可以通过屏幕尺寸来分别获取不同的value文件下所对应的布局文件别名。

6、使用方向限定符

有的时候我们的应用可能需要翻转手机能获得更好的效果,此时的布局也需要做相应的改变,也可以分别做出对应的布局文件。请参照上面的表格(land  和 port)。

7、使用Nine-Patch图

对于不同的屏幕尺寸通常我们需要使用不同尺寸的图片资源,所以在设计可变大小的组件时,一定要使用Nine-Patch图。

如上图的Nine-Patch图在不同尺寸手机上的拉伸效果如下:

有关Nine-Patch的制作方法请参考我的另一篇博文:http://blog.csdn.net/dawanganban/article/details/17379193

二、支持不同的屏幕密度

我们在设计布局的时候不能使用绝对的像素尺寸,而应该使用dp和sp.不同设备具有不同的屏幕密度,1英寸的不同密度手机上的像素个数是不同的。为了让大家搞清楚这些基本概念,我逐一解释一下:

dpi 像素密度:像素密度,即每英寸屏幕所拥有的像素数,像素密度越大,显示画面细节就越丰富。

我们可以用这个公式表示  1dpi = 1pix / 1in

dp (dip)设备独立像素:在屏幕密度为160的显示屏上,1dip=1px

有了这些概念,我们来算一下,1dip是多长(是多少英寸)

result = in / dip

result = in / px  (根据dp的定义)

result = 1 / (px / in)

result = 1 / dpi

result = 1 / 160

从上面的推导可以看出在屏幕密度为160的显示屏上 1dip其实就是 1 / 160 英寸,是一个屏幕尺寸上的绝对单位。

Android中为我们提供了适配不同分辨率的资源包,我们只需要做一套资源就可以自动帮我们换算成相应dpi(分辨率)下的尺寸,放大及缩小比例如上图所示。

三、不同UI的代码逻辑适配

1、确定当前布局

public  class  NewsReaderActivity  extends  FragmentActivity  { boolean mIsDualPane ; @Override public  void onCreate ( Bundle savedInstanceState )  { super . onCreate ( savedInstanceState ); setContentView ( R . layout . main_layout ); View articleView = findViewById ( R . id . article ); mIsDualPane = articleView !=  null  &&  articleView . getVisibility ()  ==  View . VISIBLE ; }
}

如上面代码,我们可以通过某些View的可见性来判断选用的布局文件。

2、根据不同的布局,做出不同的逻辑

@Override
public void onHeadlineSelected(int index) {mArtIndex = index;if (mIsDualPane) {/* display article on the right pane */mArticleFragment.displayArticle(mCurrentCat.getArticle(index));} else {/* start a separate activity */Intent intent = new Intent(this, ArticleActivity.class);intent.putExtra("catIndex", mCatIndex);intent.putExtra("artIndex", index);startActivity(intent);}
}

3、使用代码片段实现重用

<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>

设计兼容不同的屏幕尺寸的Android界面相关推荐

  1. 为任意屏幕尺寸构建 Android 界面

    /   今日科技快讯   / 北京时间2022年1月17日,我国在太原卫星发射中心用长征二号丁运载火箭,成功将试验十三号卫星发射升空,卫星顺利进入预定轨道,发射任务获得圆满成功. /   前言   / ...

  2. 创建支持多种屏幕尺寸的Android应用

    负责人:冰凝 原文链接:http://developer.android.com/guide/practices/screens_support.html Android涉及各种各样的支持不同屏幕尺寸 ...

  3. android 布局 不同屏幕大小,如何创建支持不同屏幕尺寸的Android应用

    Supporting Different Screens Android用两种常规属性来分类设备屏幕:尺寸和像素密度.作为开发者,你应当预料到你的android应用程序将会被安装到不同屏幕尺寸和像素密 ...

  4. 如何创建支持不同屏幕尺寸的Android应用(转载)

    2019独角兽企业重金招聘Python工程师标准>>> 源地址:http://wiki.eoe.cn/page/Supporting_Different_Screens Suppor ...

  5. 安卓和ios的ui设计区别_浅谈IOS和Android界面设计尺寸规范

    刚开始接触UI的时候,碰到的最多的就是尺寸问题,什么画布要建多大,文字该用多大才合适,我要做几套界面才可以?什么七七八八的也着实让人有些头疼. 废话不多说,希望大家耐心看完后,不要再纠结于尺寸相关的东 ...

  6. android 回收站设计,android-如何根据屏幕尺寸设置回收站视图项目的宽度尺寸?...

    我想让我的RecyclerView项目的宽度和高度相等,并根据屏幕大小动态更改其大小吗? 我该如何处理任何建议或示例代码? 这是我的代码: mrv = findViewById(R.id.rv_seg ...

  7. 2021年最全Android屏幕适配的度量单位px dp(dip) ppi dpi sp pt的区别(最详细,屏幕尺寸,屏幕分辨率,屏幕兼容,屏幕适配)

    0 前言 快乐李同学最新在学习Android屏幕适配相关的知识点,其中涉及到了很多Android屏幕适配相关概念和度量单位,但是快乐李同学掌握的相关知识不牢固,于是决定写出这两篇文章对这方面的知识点做 ...

  8. Supporting Multiple Screens(支持Android各种屏幕尺寸)

    原文链接 本文翻译自Supporting Multiple Screens. 目前我这篇翻译应该是最全,最完整的官方文档翻译,转载我的翻译请联系本人,标明出处. Supporting Multiple ...

  9. 屏幕适配(刘海屏、18:9屏幕尺寸、隐藏SystemUI)

    刘海屏 萨瑞刘海屏API接口    <meta-data android:name="android.compatible_notch" android:value=&quo ...

最新文章

  1. Scala’s parallel collections
  2. 如何快速打造高性能对话式AI?这个NLP公开课带你从入门到进阶,快来报名
  3. tab选项卡在鼠标经过时实现切换延迟
  4. c++中delete对象后 调用成员函数_C++类的特殊成员函数及default/delete特性
  5. 关于HTML、js加密、混淆、源码保护、代码安全,防止解压直接看源码
  6. Python学习笔记之异常
  7. linux rpm找不到命令_linux书后习题(4-9章不全) - lijinli
  8. Android ActionBar示例教程
  9. .NET dnSpy 程序集编辑器,反编译器和调试器
  10. extjs中rowEditing动态编辑
  11. 农田生产潜力数据集、耕地分布矢量数据、土地利用数据、作物分布数据、林地分布数据、绿地分布
  12. linux plc编程软件安装,西门子PLC编程软件S7-200 v4.0.8官方版
  13. excel查询mysql数据库表,?如何通过Excel查询MySQL数据库
  14. MMORPG开发之七日创世说(二)
  15. [BTS] Unable to create the transform
  16. 厦门大学计算机科学学院,厦门大学张俊松
  17. 南宁计算机职称考试网,南宁人事考试职称网
  18. wps excel 表格粘贴到 word 删除首行缩进
  19. RFID 中间件规范 -- ALE 介绍
  20. NOIP 2018 滚粗记(bushi)

热门文章

  1. 固高Otostudio软件使用基础知识
  2. 摄影教室:数码摄影区域曝光法实用教程
  3. Qt Qml 汽车仪表
  4. 玩电脑玩出的 Linux 专家 - Google(谷歌)开发者日演讲者之苏哲
  5. python热图_python – 使用matplotlib中的3D数据生成热图
  6. Unity之UGUI详解
  7. 都整理好了,总有一个你用得上
  8. 1934: [Shoi2007]Vote 善意的投票
  9. 穿越美丽秋色---黄草梁
  10. java环境变量配置验证,java环境变量配置