android屏幕自适应的四种方法_android 设置自适应窗口-程序员宅基地

技术标签: android  

转自:http://www.eoeandroid.com/home.php?mod=space&uid=636088&do=blog&id=48749

一、细说layout_weight

    目前最为推荐的Android多屏幕自适应解决方案。
    该属性的作用是决定控件在其父布局中的显示权重,一般用于线性布局中。其值越小,则对应的layout_width或layout_height的优先级就越高,一般横向布局中,决定的是 layout_width 的优先级;纵向布局中,决定的是 layout_height 的优先级。
    传统的layout_weight使用方法是将当前控件的layout_widthlayout_height都设置成fill_parent,这样就可以把控件的显示比例完全交给layout_weight;这样使用的话,就出现了layout_weight越小,显示比例越大的情况。不过对于2个控件还好,如果控件过多,且显示比例也不相同的时候,控制起来就比较麻烦了,毕竟反比不是那么好确定的。
    于是就有了现在最为流行的 0px设值法。看似让人难以理解的layout_height=0px的写法,结合layout_weight,却可以使控件成正比例显示,轻松解决了当前Android开发最为头疼的碎片化问题之一。
    先看下面的stylesstyle_layout.xml
?
代码片段,双击复制

[代码]php代码:

01 <?xml version="1.0" encoding="utf-8"?>
02 <resources> 
03    
04 <!-- 全屏幕拉伸-->
05   <style name="layout_full"
06     <item name="android:layout_width">fill_parent</item> 
07     <item name="android:layout_height">fill_parent</item> 
08   </style>
09      
10 <!-- 固定自身大小-->
11   <style name="layout_wrap"
12     <item name="android:layout_width">wrap_content</item> 
13     <item name="android:layout_height">wrap_content</item> 
14   </style>
15    
16 <!-- 横向分布-->
17   <style name="layout_horizontal" parent="layout_full"
18     <item name="android:layout_width">0px</item> 
19   </style>
20       
21 <!-- 纵向分布-->
22   <style name="layout_vertical" parent="layout_full"
23     <item name="android:layout_height">0px</item> 
24   </style>
25            
26 </resources>

 
可以看到,layout_widthlayout_height两个属性被我封装成了4style
    根据实际布局情况,选用当中的一种,不需要自己设置,看过我前一个ActivityGroupDemo的同学应该非常熟悉了
    然后我的Demo的布局如下(weight_layout.xml

[代码]php代码:

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03         style="@style/layout_full"
04         android:orientation="vertical">
05         <LinearLayout 
06                 style="@style/layout_vertical"
07                 android:layout_weight="1"
08                 android:orientation="horizontal">
09                  <View
10                          style="@style/layout_horizontal"
11                          android:background="#aa0000"
12                          android:layout_weight="1"/>
13                  <View
14                          style="@style/layout_horizontal"
15                          android:background="#00aa00"
16                          android:layout_weight="4"/>
17                  <View
18                          style="@style/layout_horizontal"
19                          android:background="#0000aa"
20                          android:layout_weight="3"/>
21                  <View
22                          style="@style/layout_horizontal"
23                          android:background="#aaaaaa"
24                          android:layout_weight="2"/>                 
25         </LinearLayout> 
26         <LinearLayout 
27                 style="@style/layout_vertical"
28                 android:layout_weight="2"
29                 android:orientation="vertical">
30                 <View
31                          style="@style/layout_vertical"
32                          android:background="#ffffff"
33                          android:layout_weight="4"/>        
34                  <View
35                          style="@style/layout_vertical"
36                          android:background="#aa0000"
37                          android:layout_weight="3"/>
38                  <View
39                          style="@style/layout_vertical"
40                          android:background="#00aa00"
41                          android:layout_weight="2"/>
42                  <View
43                          style="@style/layout_vertical"
44                          android:background="#0000aa"
45                          android:layout_weight="1"/>
46    
47         </LinearLayout>
48 </LinearLayout>
二、自定义尺寸法
    这个是我自己想出来的方法,可能是个比较笨的方法,所以没有多少人提过用这种方法解决自适应的问题。虽然这个方法缺点也很多,但有时候也是个不错的方法。
    先看下面这张图


     可以看到我定义了两套尺寸文件,我们可以看下其中一个文件
?

[代码]php代码:

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

 
    这个是 values-480x320 文件夹下 dimens_height.xml 文件中的代码,我把整个高度分成了 80等分,这是因为大部分屏幕的宽度或高度都是80的整数倍(个别特殊的除外),不同的等分在不同的分辨率中设定不同的尺寸值。
    由于每一套界面都要写一套,所以有些同学可能觉着不太好,不过这个写起来比较简单,而且以后也不用改,所以有时候也可以考虑用一下!
    再看我Demo的布局代码(dimen_layout.xml
?

[代码]php代码:

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03         android:layout_width="fill_parent"
04          android:layout_height="fill_parent"
05         android:orientation="vertical">
06                  <View
07                          android:layout_width="@dimen/width_76_80"
08                          android:layout_height="@dimen/height_10_80"
09                          android:background="#ffcccc"
10                          android:layout_margin="@dimen/width_2_80"/>       
11         <LinearLayout
12                  android:layout_width="fill_parent"
13                  android:layout_height="fill_parent">
14                  <View
15                          android:layout_width="@dimen/width_30_80"
16                          android:layout_height="@dimen/height_50_80"
17                          android:background="#ccccff"
18                          android:layout_margin="@dimen/height_5_80"/>
19                  <LinearLayout
20                          android:layout_width="fill_parent"
21                          android:layout_height="fill_parent"
22                          android:orientation="vertical">       
23                          <Button
24                                  android:layout_width="@dimen/width_30_80"
25                                  android:layout_height="@dimen/height_5_80"
26                                  android:background="#ccffcc"
27                                  android:layout_marginBottom="@dimen/height_1_80"
28                                  android:text="5"/>
29                          <Button
30                                  android:layout_width="@dimen/width_30_80"
31                                  android:layout_height="@dimen/height_10_80"
32                                  android:background="#ccffcc"
33                                  android:layout_marginBottom="@dimen/height_1_80"
34                                  android:text="10"/>
35                          <Button
36                                  android:layout_width="@dimen/width_30_80"
37                                  android:layout_height="@dimen/height_15_80"
38                                  android:background="#ccffcc"
39                                  android:layout_marginBottom="@dimen/height_1_80"
40                                  android:text="15"/>
41                          <Button
42                                  android:layout_width="@dimen/width_30_80"
43                                  android:layout_height="@dimen/height_20_80"
44                                  android:background="#ccffcc"
45                                  android:text="20"/>
46                  </LinearLayout>       
47          </LinearLayout>               
48 </LinearLayout>

 

    以上是我写的统一的布局代码,来看下在两个不同分辨率的模拟器上的显示效果吧(大家注意我的代码中有 margin 这样的值也用到了自定义尺寸,如果这个 margin 使用 layout_weight 来控制的话,无疑要多嵌套一层线性布局,所以说没有一个方法是十全十美的,这第 2 个方法有时候用起来反而还要方便一些)





三、java代码中设置宽高度
    也许很多人会反对这种方法,因为即使是官方也是推荐使用xml的方式写布局。不过我们在这不会像Swing那样写那么多麻烦的布局代码,因为我们只是在代码中重新设定控件的宽高度而已,其他属性依然是交给xml布局文件的。这个方法其实是我跟同事偷学来的,虽然我不赞成这样的方法,但他确确实实也是解决屏幕自适应问题的方案之一,而且它没我想象的那么复杂,其实很简单。
    首先我们要做的是获取当前屏幕的宽高度,因为这个在后面要用到
    我们可以写两个静态变量用来保存当前屏幕的宽高度:
?

[代码]php代码:

1 ic class Constant {
2         public static int displayWidth;  //屏幕宽度
3         public static int displayHeight; //屏幕高度
4 }


    然后在第一个 Activity 启动的时候,获取这两个值
?

[代码]php代码:

1 DisplayMetrics displayMetrics = new DisplayMetrics();
2         getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
3         Constant.displayWidth = displayMetrics.widthPixels;
4         Constant.displayHeight = displayMetrics.heightPixels;


    布局代码我们可以全都统一写成 wrap-content,其实写成什么都无所谓,因为这个值只是暂时的
?

[代码]php代码:

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:orientation="vertical"
04     android:layout_width="fill_parent"
05     android:layout_height="fill_parent">
06 <Button 
07         android:id="@+id/btn1"
08     android:layout_width="wrap_content"
09     android:layout_height="wrap_content"
10     android:background="#ffcccc"
11     android:text="aaaaaaaa"/>
12 <Button 
13         android:id="@+id/btn2"
14     android:layout_width="wrap_content"
15     android:layout_height="wrap_content"
16     android:background="#ccffcc"
17     android:text="bbbbbbbbb"/>
18 <Button 
19         android:id="@+id/btn3"
20     android:layout_width="wrap_content"
21     android:layout_height="wrap_content"
22     android:background="#ccccff"
23     android:text="ccccccccc"/>
24 <Button 
25         android:id="@+id/btn4"
26     android:layout_width="wrap_content"
27     android:layout_height="wrap_content"
28     android:background="#ffffcc"
29     android:text="dddddddddddddddddd"/>  
30 </LinearLayout>

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

[代码]php代码:

01 // 第一个按钮,宽度100%,高度10%
02                 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
03                                 LayoutParams.FILL_PARENT,
04                                 (int) (Constant.displayHeight * 0.1f + 0.5f));
05                 btn1.setLayoutParams(params);
06                 // 第二个按钮,宽度100%,高度30%
07                 LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
08                                 LayoutParams.FILL_PARENT,
09                                 (int) (Constant.displayHeight * 0.3f + 0.5f));
10                 btn2.setLayoutParams(params2);
11                 // 第三个按钮,宽度50%,高度20%
12                 LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(
13                                 (int) (Constant.displayWidth * 0.5f + 0.5f),
14                                 (int) (Constant.displayHeight * 0.2f + 0.5f));
15                 btn3.setLayoutParams(params3);
16                 // 第三个按钮,宽度70%,高度填满剩下的空间
17                 LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(
18                                 (int) (Constant.displayWidth * 0.7f + 0.5f),
19                                 LayoutParams.FILL_PARENT);
20                 btn4.setLayoutParams(params4);

 
    大家可以看到其实代码并不复杂,都能看得懂



多布局
    做为最后的方法,也是最后一个才会考虑的方法,那就是为不同的尺寸界面单独写布局。不到万不得已不要用这个方法,相信不少人和我一样都被逼着用过这个方法吧。需要说明的是,横竖屏切换使用不同布局也是用这个方法解决的;代码我就不上了,给大家看两张图吧,一个是1个布局的,一个是写了多布局的,大家一看就明白了
补充一下,写多个布局的时候,配置文件一定要加上这段配置代码,不然有时可能会出问题
    <supports-screens android:largeScreens="true"
                android:normalScreens="true" android:anyDensity="true" />



五、其他
    以上说的都是多个屏幕显示相同内容需要考虑的问题,还有一种是在不同的屏幕上显示内容不同的情况,其实这个问题我们往往是用滚动视图来解决的,也就是ScrowView;需要注意的是ScrowView中使用layout_weight是无效的,既然使用ScrowView了,就把它里面的控件的大小都设成固定的吧。
    此外关于图片的自适应问题,主要是2点,一个是9patch图,这个东西大家都要学会去做,不难;不过有些编译器在识别9patch图时会出这样那样的bug,像我的Eclipse就不认这个,而同一个9patch图在别的电脑上却是没问题的,
    第二个要说的是我曾经被困扰的一个问题,对于480x800  480x854这两个尺寸,他们显示同一个图片时,总有一个会拉伸(如果9patch可以解决的还好)。其实当初困扰我的是,这两个尺寸都是hdpi的,以为无法给这两个屏幕做不同的图片。后来无意中发现,图片可以和布局一样分多个尺寸的,而不仅仅是根据密度分,也就是说你可以写这样的文件夹drawable-hdpi-800x480drawable-hdpi-854x480,在它们里面放不同的图片,这样图片也能自适应了。
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/opzoonzhuzhengke/article/details/30217999

智能推荐

python difflib 编辑距离_Python Edit_Distance包_程序模块 - PyPI - Python中文网-程序员宅基地

文章浏览阅读413次。编辑距离用于计算序列之间编辑距离和对齐的python模块。我需要一种方法来计算python中序列之间的编辑距离。我没有能够找到任何合适的库来实现这一点,所以我自己编写了一个。在那里似乎有许多可用于计算编辑的编辑距离库两个字符串之间的距离,但不是两个序列之间的距离。这完全是用python编写的。这种实现可能是在python中优化为更快。如果在C中实现。库API是根据difflib.sequencem..._edit distance python lib

antd upload组件 手动上传-程序员宅基地

文章浏览阅读3.8k次,点赞2次,收藏15次。antd 的upload组件是点开对话框后,按下确实就会上传,而且如果多选文件也会反复调用后端接口来完成上传。因为项目需要,所以要实现手动上传,和一次性上传多个文件(调用一次后端接口)在实现这个功能时,我翻阅了很多博客,可能是因为版本原因,很多代码都无用,最后还是通过翻阅官方文档,才最终实现。..._antd upload

sqlite3 环境搭建_sqlite 部署-程序员宅基地

文章浏览阅读246次。注意 第一步在一个文件下打开终端然后 sqlite3 student.db(创建一个数据库),然后再create stu。callback 回调函数 (只有sql为查询语句的时候,才会执行此语句)6--删除一列(sqlite3 不支持) 用下面方法。功能 :打开sqlite 数据库。功能 :关闭sqlite 数据库。基本sql命令,不以 . 夹头,db:指向sqlite句柄的指针。将新表的名字改为原来表的名字。sqlite3的基本命令。功能:执行一条sql语句。以 . 开头的命令。_sqlite 部署

canal-adapter趟坑实践:canal-server的kafka SASLPLAIN方式鉴权适配_canal adapter kafka sasl-程序员宅基地

文章浏览阅读1.4w次。前言canal-server同步到kafka本身是支持Kerberos方式的鉴权的,但是鉴于项目现在使用的kafka集群使用的是SASL/PLAIN的鉴权方式,所以需要对canal-server同步kafka做一下适配改造。准备kafka SASL/PLAIN鉴权的搭建我参考的这篇文章kafka SASL/PLAIN鉴权的搭建了解如何使用java向以SASL/PLAIN方式鉴权的kafk..._canal adapter kafka sasl

Android adb shell相关命令_android的shell命令工具:设备规范管理-程序员宅基地

文章浏览阅读711次。adb(调试桥):debug工具。adb作用:借助adb工具,可以管理设备或手机模拟器状态。adb相关操作命令如下: 1. 显示系统中全部Android平台: android list targets2. 显示系统中全部AVD(模拟器): android list avd3. 创建AVD(模拟器): android create avd_android的shell命令工具:设备规范管理

Centos 7.9 在线安装 VirtualBox 7.0_centos安装virtualbox-程序员宅基地

文章浏览阅读769次,点赞10次,收藏7次。Centos 7.9 在线安装 VirtualBox 7.0_centos安装virtualbox

随便推点

Autodesk官方卸载工具软件安装教程-程序员宅基地

文章浏览阅读1.4w次,点赞9次,收藏10次。Autodesk卸载工具是一个专门用于Autodesk软件的卸载工具,可以自动识别电脑中的所有Autodesk软件,只需一键点击就能将Autodesk的软件完美卸载,并且不保留任何痕迹,这款卸载工具就可以帮助用户全面卸载Autodesk软件。_autodesk官方卸载工具

JDBC报错:Cannot find class: com.mysql.jdbc.Driver-程序员宅基地

文章浏览阅读4.9k次。1.配置书写错误:配置文件value值引号内不能有空格,属性文件配置信息末尾不能有空格(1)打开属性文件中com.mysql.jdbc.Driver后发现多了一个空格(如下我标出了),所以写属性文件时一定别多输入多余的空格了。 jdbc.driverClassName=com.mysql.jdbc.Driver(此处有空格)(2)配置文件中的value值的" "号中前面或..._cannot find class: com.mysql.jdbc.driver

软件常用术语_软件术语-程序员宅基地

文章浏览阅读1.8k次。软件常用术语,免得你面对各种设计模式头发晕_软件术语

Machine Learning 2 - 非线性回归算法分析_非线性回归分析方法-程序员宅基地

文章浏览阅读2.8k次。2017-08-02@erixhao 技术极客TechBoosterAI 机器学习第二篇 - 非线形回归分析。我们上文深入本质了解了机器学习基础线性回归算法后,本文继续研究非线性回归。非线性回归在机器学习中并非热点,并且较为小众,且其应用范畴也不如其他广。鉴于此,我们本文也将较为简单的介绍,并不会深入展开。非线性回归之后,我们会继续经典机器学习算法包括决策_非线性回归分析方法

hive基本函数_josn mincol-程序员宅基地

文章浏览阅读164次。一、关系运算:1.等值比较: =语法:A=B操作类型:所有基本类型描述:如果表达式A与表达式B相等,则为TRUE;否则为FALSE举例:hive>select 1 from lxw_dual where 1=1;12.不等值比较: <>语法: A <> B操作类型:所有基本类型描述:如果表达式A为NULL,或者表..._josn mincol

FI 与SD MM相关接口配置_sd 和fi 接口产生什么凭证?-程序员宅基地

文章浏览阅读767次。1 FI/SD 借口配置FI/SD通过tcode VKOA为billing设置过帐科目,用户可以创建自己的科目定义数据表。 科目是做到COA级的,通过KOFI/KOFK这两个condition type确定分别过帐到FI和CO凭证中。 由于PricingProc.是同Sale_sd 和fi 接口产生什么凭证?

推荐文章

热门文章

相关标签