四种必须知道的Android屏幕自适应解决方案

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 全屏幕拉伸-->
<style name="layout_full">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
</style>
<!-- 固定自身大小-->
<style name="layout_wrap">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<!-- 横向分布-->
<style name="layout_horizontal" parent="layout_full">
<item name="android:layout_width">0px</item>
</style>
<!-- 纵向分布-->
<style name="layout_vertical" parent="layout_full">
<item name="android:layout_height">0px</item>
</style>
</resources>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/layout_full"
android:orientation="vertical">
<LinearLayout
style="@style/layout_vertical"
android:layout_weight="1"
android:orientation="horizontal">
<View
style="@style/layout_horizontal"
android:background="#aa0000"
android:layout_weight="1"/>
<View
style="@style/layout_horizontal"
android:background="#00aa00"
android:layout_weight="4"/>
<View
style="@style/layout_horizontal"
android:background="#0000aa"
android:layout_weight="3"/>
<View
style="@style/layout_horizontal"
android:background="#aaaaaa"
android:layout_weight="2"/>
</LinearLayout>
<LinearLayout
style="@style/layout_vertical"
android:layout_weight="2"
android:orientation="vertical">
<View
style="@style/layout_vertical"
android:background="#ffffff"
android:layout_weight="4"/>
<View
style="@style/layout_vertical"
android:background="#aa0000"
android:layout_weight="3"/>
<View
style="@style/layout_vertical"
android:background="#00aa00"
android:layout_weight="2"/>
<View
style="@style/layout_vertical"
android:background="#0000aa"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>

整个界面布局看起来非常直观,只是嵌套的逻辑要自己理下。显示效果如下图,其中左面一个是480x800的界面,右面的是320x480的界面(后面的图也如此),可以看出显示比例和代码中完全一致,我就不多说了,大家对照下就能看出来了。
可以看到我定义了两套尺寸文件,我们可以看下其中一个文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="height_1_80">6px</dimen><dimen name="height_2_80">12px</dimen>
<dimen name="height_3_80">18px</dimen><dimen name="height_4_80">24px</dimen>
<dimen name="height_5_80">30px</dimen><dimen name="height_6_80">36px</dimen>
<dimen name="height_7_80">42px</dimen><dimen name="height_8_80">48px</dimen>
<dimen name="height_9_80">54px</dimen><dimen name="height_10_80">60px</dimen>
<dimen name="height_11_80">66px</dimen><dimen name="height_12_80">72px</dimen>
<dimen name="height_13_80">78px</dimen><dimen name="height_14_80">84px</dimen>
<dimen name="height_15_80">90px</dimen><dimen name="height_16_80">96px</dimen>
<dimen name="height_17_80">102px</dimen><dimen name="height_18_80">108px</dimen>
<dimen name="height_19_80">114px</dimen><dimen name="height_20_80">120px</dimen>
<dimen name="height_21_80">126px</dimen><dimen name="height_22_80">132px</dimen>
<dimen name="height_23_80">138px</dimen><dimen name="height_24_80">144px</dimen>
<dimen name="height_25_80">150px</dimen><dimen name="height_26_80">156px</dimen>
<dimen name="height_27_80">162px</dimen><dimen name="height_28_80">168px</dimen>
<dimen name="height_29_80">174px</dimen><dimen name="height_30_80">180px</dimen>
<dimen name="height_31_80">186px</dimen><dimen name="height_32_80">192px</dimen>
<dimen name="height_33_80">198px</dimen><dimen name="height_34_80">204px</dimen>
<dimen name="height_35_80">210px</dimen><dimen name="height_36_80">216px</dimen>
<dimen name="height_37_80">222px</dimen><dimen name="height_38_80">228px</dimen>
<dimen name="height_39_80">234px</dimen><dimen name="height_40_80">240px</dimen>
<dimen name="height_41_80">246px</dimen><dimen name="height_42_80">252px</dimen>
<dimen name="height_43_80">258px</dimen><dimen name="height_44_80">264px</dimen>
<dimen name="height_45_80">270px</dimen><dimen name="height_46_80">276px</dimen>
<dimen name="height_47_80">282px</dimen><dimen name="height_48_80">288px</dimen>
<dimen name="height_49_80">294px</dimen><dimen name="height_50_80">300px</dimen>
<dimen name="height_51_80">306px</dimen><dimen name="height_52_80">312px</dimen>
<dimen name="height_53_80">318px</dimen><dimen name="height_54_80">324px</dimen>
<dimen name="height_55_80">330px</dimen><dimen name="height_56_80">336px</dimen>
<dimen name="height_57_80">342px</dimen><dimen name="height_58_80">348px</dimen>
<dimen name="height_59_80">354px</dimen><dimen name="height_60_80">360px</dimen>
<dimen name="height_61_80">366px</dimen><dimen name="height_62_80">372px</dimen>
<dimen name="height_63_80">378px</dimen><dimen name="height_64_80">384px</dimen>
<dimen name="height_65_80">390px</dimen><dimen name="height_66_80">396px</dimen>
<dimen name="height_67_80">402px</dimen><dimen name="height_68_80">408px</dimen>
<dimen name="height_69_80">414px</dimen><dimen name="height_70_80">420px</dimen>
<dimen name="height_71_80">426px</dimen><dimen name="height_72_80">432px</dimen>
<dimen name="height_73_80">438px</dimen><dimen name="height_74_80">444px</dimen>
<dimen name="height_75_80">450px</dimen><dimen name="height_76_80">456px</dimen>
<dimen name="height_77_80">462px</dimen><dimen name="height_78_80">468px</dimen>
<dimen name="height_79_80">474px</dimen><dimen name="height_80_80">480px</dimen>
</resources>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<View
android:layout_width="@dimen/width_76_80"
android:layout_height="@dimen/height_10_80"
android:background="#ffcccc"
android:layout_margin="@dimen/width_2_80"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<View
android:layout_width="@dimen/width_30_80"
android:layout_height="@dimen/height_50_80"
android:background="#ccccff"
android:layout_margin="@dimen/height_5_80"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="@dimen/width_30_80"
android:layout_height="@dimen/height_5_80"
android:background="#ccffcc"
android:layout_marginBottom="@dimen/height_1_80"
android:text="5"/>
<Button
android:layout_width="@dimen/width_30_80"
android:layout_height="@dimen/height_10_80"
android:background="#ccffcc"
android:layout_marginBottom="@dimen/height_1_80"
android:text="10"/>
<Button
android:layout_width="@dimen/width_30_80"
android:layout_height="@dimen/height_15_80"
android:background="#ccffcc"
android:layout_marginBottom="@dimen/height_1_80"
android:text="15"/>
<Button
android:layout_width="@dimen/width_30_80"
android:layout_height="@dimen/height_20_80"
android:background="#ccffcc"
android:text="20"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

以上是我写的统一的布局代码,来看下在两个不同分辨率的模拟器上的显示效果吧(大家注意我的代码中有margin这样的值也用到了自定义尺寸,如果这个margin使用layout_weight来控制的话,无疑要多嵌套一层线性布局,所以说没有一个方法是十全十美的,这第2个方法有时候用起来反而还要方便一些)
public class Constant {
public static int displayWidth; //屏幕宽度
public static int displayHeight; //屏幕高度
}
然后在第一个Activity启动的时候,获取这两个值
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
Constant.displayWidth = displayMetrics.widthPixels;
Constant.displayHeight = displayMetrics.heightPixels;
布局代码我们可以全都统一写成wrap-content,其实写成什么都无所谓,因为这个值只是暂时的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffcccc"
android:text="aaaaaaaa"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ccffcc"
android:text="bbbbbbbbb"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ccccff"
android:text="ccccccccc"/>
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffcc"
android:text="dddddddddddddddddd"/>
</LinearLayout>

最后我们在Activity的onCreate方法里这么做

// 第一个按钮,宽度100%,高度10%
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
(int) (Constant.displayHeight * 0.1f + 0.5f));
btn1.setLayoutParams(params);
// 第二个按钮,宽度100%,高度30%
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
(int) (Constant.displayHeight * 0.3f + 0.5f));
btn2.setLayoutParams(params2);
// 第三个按钮,宽度50%,高度20%
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(
(int) (Constant.displayWidth * 0.5f + 0.5f),
(int) (Constant.displayHeight * 0.2f + 0.5f));
btn3.setLayoutParams(params3);
// 第三个按钮,宽度70%,高度填满剩下的空间
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(
(int) (Constant.displayWidth * 0.7f + 0.5f),
LayoutParams.FILL_PARENT);
btn4.setLayoutParams(params4);

补充一下,写多个布局的时候,配置文件一定要加上这段配置代码,不然有时可能会出问题
<supports-screens android:largeScreens="true"
android:normalScreens="true" android:anyDensity="true" />
<ignore_js_op>
四种必须知道的Android屏幕自适应解决方案的更多相关文章
- 4种必须知道的Android屏幕自适应解决方案
文章来源:http://blog.csdn.net/shimiso/article/details/19166167 demo下载:http://www.eoeandroid.com/forum.ph ...
- [你必须知道的.NET]第二十四回:认识元数据和IL(上)
发布日期:2009.02.24 作者:Anytao © 2009 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. 说在,开篇之前 很早就有说说Metadata(元数据)和IL(中 ...
- 转15个必须知道的chrome开发者技巧GIF
在Web开发者中,Google Chrome是使用最广泛的浏览器.六周一次的发布周期和一套强大的不断扩大开发功能,使其成为了web开发者必备的工具.你可能已经熟悉了它的部分功能,如使用console和 ...
- 15个必须知道的chrome开发者技巧(转)
15个必须知道的chrome开发者技巧 在Web开发者中,Google Chrome是使用最广泛的浏览器.六周一次的发布周期和一套强大的不断扩大开发功能,使其成为了web开发者必备的工具.你可能已经熟 ...
- Webservice WCF WebApi 前端数据可视化 前端数据可视化 C# asp.net PhoneGap html5 C# Where 网站分布式开发简介 EntityFramework Core依赖注入上下文方式不同造成内存泄漏了解一下? SQL Server之深入理解STUFF 你必须知道的EntityFramework 6.x和EntityFramework Cor
Webservice WCF WebApi 注明:改编加组合 在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下, ...
- 《你必须知道的.NET》读书笔记一:小OO有大智慧
此篇已收录至<你必须知道的.Net>读书笔记目录贴,点击访问该目录可以获取更多内容. 一.对象 (1)出生:系统首先会在内存中分配一定的存储空间,然后初始化其附加成员,调用构造函数执行初 ...
- 《你必须知道的.NET》读书笔记二:小OO有大原则
此篇已收录至<你必须知道的.Net>读书笔记目录贴,点击访问该目录可以获取更多内容. 一.单一职责原则 (1)核心思想:一个类最好只做一件事,只有一个引起它变化的原因 (2)常用模式:Fa ...
- [你必须知道的.NET] 第八回:品味类型---值类型与引用类型(上)-内存有理
原文地址:http://kb.cnblogs.com/page/42318/ 系列文章导航: [你必须知道的.NET] 开篇有益 [你必须知道的.NET] 第一回:恩怨情仇:is和as [你必须知道的 ...
- 从零开始学习jQuery(剧场版) 你必须知道的javascript
原文:从零开始学习jQuery(剧场版) 你必须知道的javascript 一.摘要 本文是jQuery系列教程的剧场版, 即和jQuery这条主线无关, 主要介绍大家平时会忽略的一些javascri ...
随机推荐
- 使用 Date 和 SimpleDateFormat 类表示时间
在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的Date类.这个类最主要的作用就是获取当前时间,我们来看下Date的类的使用: Date d=new Dat ...
- MySQL索引视图
一.索引 索引是存放在模式(schema)中的一个数据库对象,索引的作用就是提高对表的检索查询速度, 索引是通过快速访问的方法来进行快速定位数据,从而减少了对磁盘的读写操作. 索引是数据库的一个对象, ...
- ACM——2的n次方
2的N次方 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte 总提交:1715 测试通过:838 描述 编程精确计算2 ...
- 使用Fragment实现类似TabHost标签栏的效果
在前几天,我写了篇<Android TabHost的使用>简单的介绍了其使用的方法,但是在实现的时候发现TabHost已经被官方遗弃了.虽然我觉得TabHost还是多好用的(可能因为我这种 ...
- virtualbox安装ubuntu出现“The system is running in low-graphics mode”
cd /etc/X11 sudo mv xorg.conf.failsafe xorg.conf sudo reboot 即可.
- MyEclipse8.5注册码生成
步骤: 1.建立一个任意名称的Java project: 2.在该工程中建立一个名为MyEclipseGen的Java文件(MyEclipseGen.java) 3.运行下边代码,会在控制台出现&qu ...
- 深入理解Javascript之this关键字
深入理解Javascript之this关键字 作者: Laruence( ) 本文地址: http://www.laruence.com/2009/09/08/1076.html 转载请注明出处 ...
- thinkphp 缓存写入失败,网站报错
周末,正在家里休息,同事突然call 我,说网站打不开了,网站一直很正常的,突然成这样,肯定某个地方出问题了, 原来是网站所在的硬盘分区,没空间了,被mysql的日志占满了!!! 哎,好好的周末,在公 ...
- C# DES
using System; //这个是使用DES的基础 using System.Security.Cryptography; //这个是处理文字编码的前提 using System.Text; // ...
- yii2源码学习笔记(十一)
Controller控制器类,是所有控制器的基类,用于调用模型和布局. <?php /** * @link http://www.yiiframework.com/ * @copyright C ...