android:屏幕自适应之dimen使用
From:4种必须知道的Android屏幕自适应解决方案(求投票支持)
demo下载:http://www.eoeandroid.com/forum.php?mod=attachment&aid=NjE0Njh8ZTIyZDA2M2N8MTMzODgyOTQxN3w1NzAwOTV8MTczOTcz
以下是Demo首页的预览图
![](/assets/blank.gif)
<?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>
![](/assets/blank.gif)
![](/assets/blank.gif)
<?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"><Viewstyle="@style/layout_horizontal"android:background="#aa0000"android:layout_weight="1"/><Viewstyle="@style/layout_horizontal"android:background="#00aa00"android:layout_weight="4"/><Viewstyle="@style/layout_horizontal"android:background="#0000aa"android:layout_weight="3"/><Viewstyle="@style/layout_horizontal"android:background="#aaaaaa"android:layout_weight="2"/> </LinearLayout> <LinearLayout style="@style/layout_vertical"android:layout_weight="2"android:orientation="vertical"><Viewstyle="@style/layout_vertical"android:background="#ffffff"android:layout_weight="4"/> <Viewstyle="@style/layout_vertical"android:background="#aa0000"android:layout_weight="3"/><Viewstyle="@style/layout_vertical"android:background="#00aa00"android:layout_weight="2"/><Viewstyle="@style/layout_vertical"android:background="#0000aa"android:layout_weight="1"/></LinearLayout> </LinearLayout>
![](/assets/blank.gif)
整个界面布局看起来非常直观,只是嵌套的逻辑要自己理下。显示效果如下图,其中左面一个是480x800的界面,右面的是320x480的界面(后面的图也如此),可以看出显示比例和代码中完全一致,我就不多说了,大家对照下就能看出来了。
可以看到我定义了两套尺寸文件,我们可以看下其中一个文件
![](/assets/blank.gif)
<?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>
![](/assets/blank.gif)
![](/assets/blank.gif)
<?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"><Viewandroid:layout_width="@dimen/width_76_80"android:layout_height="@dimen/height_10_80"android:background="#ffcccc"android:layout_margin="@dimen/width_2_80"/> <LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"><Viewandroid:layout_width="@dimen/width_30_80"android:layout_height="@dimen/height_50_80"android:background="#ccccff"android:layout_margin="@dimen/height_5_80"/><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"> <Buttonandroid: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"/><Buttonandroid: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"/><Buttonandroid: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"/><Buttonandroid:layout_width="@dimen/width_30_80"android:layout_height="@dimen/height_20_80"android:background="#ccffcc"android:text="20"/></LinearLayout> </LinearLayout> </LinearLayout>
![](/assets/blank.gif)
以上是我写的统一的布局代码,来看下在两个不同分辨率的模拟器上的显示效果吧(大家注意我的代码中有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,其实写成什么都无所谓,因为这个值只是暂时的
![](/assets/blank.gif)
<?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>
![](/assets/blank.gif)
最后我们在Activity的onCreate方法里这么做
![](/assets/blank.gif)
// 第一个按钮,宽度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);
![](/assets/blank.gif)
补充一下,写多个布局的时候,配置文件一定要加上这段配置代码,不然有时可能会出问题
<supports-screens android:largeScreens="true"
android:normalScreens="true" android:anyDensity="true" />
<ignore_js_op>
android:屏幕自适应之dimen使用相关推荐
- android设置布局高度自适应,4种Android屏幕自适应解决方案
Android支持多屏幕机制即用为当前设备屏幕提供一种合适的方式来共同管理并解析应用资源.本文就介绍了4中Android屏幕自适应解决方案. 一.细说layout_weight 目前最为推荐的Andr ...
- Android屏幕自适应
曾被Android屏幕自适应问题烦恼过,下面为整理总结,参考了网上很多其他文章,这里一并谢过. 1 相关概念 背景:Android碎片化. 常说的屏幕尺寸,如5寸(英寸)土豪金,这里的屏幕尺寸为物理尺 ...
- android自适应屏幕方向,Android 屏幕自适应方向尺寸与分辨率-Fun言
摘要: Android 屏幕自适应方向尺寸与分辨率,android 自适应分辨率,android 屏幕自适应,android 图片自适应,android 横屏布局,包括屏幕界面布局.多分辨率支持.获 ...
- 4种必须知道的Android屏幕自适应解决方案
以下是Demo首页的预览图 demo下载:http://www.eoeandroid.com/forum.php?mod=attachment&aid=NjE0Njh8ZTIyZDA2M2N8 ...
- 屏幕自适应之dimen使用
demo下载:http://www.eoeandroid.com/forum.php?mod=attachment&aid=NjE0Njh8ZTIyZDA2M2N8MTMzODgyOTQxN3 ...
- android 屏幕自适应
一.Android屏幕适配出现的原因 1.碎片化是Android屏幕必须适配的原因 2012年,支持Android的设备共有3997种. 2013年,支持Android的设备共有11868种. 201 ...
- unity android屏幕自适应,Android应用开发之unity打开移动摄像头,并自适应屏幕显示摄像头数据。兼容android和ios...
本文将带你了解Android应用开发之unity打开移动摄像头,并自适应屏幕显示摄像头数据.兼容android和ios,希望本文对大家学Android有所帮助. 跨平台并自适应显示摄像头数据新建工程并 ...
- android编程:调节视频画面分辨率,Android编程实现屏幕自适应方向尺寸与分辨率的方法...
本文实例讲述了Android编程实现屏幕自适应方向尺寸与分辨率的方法.分享给大家供大家参考,具体如下: Android 屏幕自适应方向尺寸与分辨率,包括屏幕界面布局.多分辨率支持.获取屏幕尺寸.屏幕横 ...
- android屏幕适配的目的,Android 不同分辨率下屏幕适配的实战方案与经验总结
Android 开发中,屏幕适配是一大考点,几乎每一场面试,都不会落下这个问题,这个问题说简单也简单,说难也难,当然对于有过真实的适配经验的人来说,这个根本不算什么问题,从坑里爬过的人,自然知道这其中 ...
最新文章
- 句法依存分析_[NLP学习笔记]句法分析
- 运行ORB-SLAM笔记_使用篇(二)
- IDEA快捷键及基本使用方法
- 51Nod-1046 A^B Mod C【快速模幂】
- MySQL的约束、多表查询、子查询
- ASP.NET AJAX客户端编程之旅(二)——知其所以然
- 【Tools】VMware Workstation 15.5 Pro安装详解
- try....exception....finally
- 马云融资80亿美金的“资本”
- python上传本地文件到远程hdfs_Python之——自动上传本地log文件到HDFS(基于Hadoop 2.5.2)...
- OpenGL网络资源
- springboot如何对本地数据库增删改查_mysql数据库基本增删改查操作总结
- C语言实现约瑟夫环代码
- 涨姿势!一文了解深度学习中的注意力机制
- 支付宝服务商模式下的支付开发
- java读取文件(java读取文件指定内容)
- My 2007 Fash game: Elite Shooter
- 网络编程中,同步传输和异步传输有什么区别
- 《快学BigData》--Linux 常用命令
- 4月2日云栖精选夜读 | 对话行癫:解密阿里云顶层设计和底层逻辑