Android 屏幕适配

本文详细介绍了Android多屏幕自适应布局的概念、原理及实现方法,包括屏幕相关概念、多屏幕支持机制、布局和图片资源适配策略,并通过实例展示了如何使用layout_weight和自定义尺寸法解决自适应问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

《理论》

1.屏幕相关概念 
1.1分辨率 
是指屏幕上有横竖各有多少个像素 
1.2屏幕尺寸 
指的是手机实际的物理尺寸,比如常用的2.8英寸,3.2英寸,3.5英寸,3.7英寸 

android将屏幕大小分为四个级别(smallnormallargeand extra large)。 

1.3屏幕密度 
      每英寸像素数 手机可以有相同的分辨率,但屏幕尺寸可以不相同, 
      Diagonal pixel表示对角线的像素值(=),DPI=933/3.7=252 
      android将实际的屏幕密度分为四个通用尺寸(lowmediumhighand extra high 

      一般情况下的普通屏幕:ldpi120dpimdpi160dpihdpi240dpixhdpi320dpi 

      对于屏幕来说,dpi越大,屏幕的精细度越高,屏幕看起来就越清楚 


2.android多屏幕支持机制 
Android的支持多屏幕机制即用为当前设备屏幕提供一种合适的方式来共同管理并解析应用资源。 
Android平台中支持一系列你所提供的指定大小(size-specific,指定密度(density-specific)的合适资源。 

指定大小(size-specific)的合适资源是指small, normal, large, and xlarge 
指定密度(density-specific)的合适资源,是ldpi (low), mdpi (medium), hdpi (high), and xhdpi (extra high). 

Android有个自动匹配机制去选择对应的布局和图片资源 
1)界面布局方面 
   根据物理尺寸的大小准备5套布局: 
    layout(放一些通用布局xml文件,比如界面顶部和底部的布局,不会随着屏幕大小变化,类似windos窗口的title bar), 
    layout-small(屏幕尺寸小于3英寸左右的布局),  
    layout-normal(屏幕尺寸小于4.5英寸左右), 
    layout-large(4英寸-7英寸之间), 
    layout-xlarge(7-10英寸之间) 
2)图片资源方面 
  需要根据dpi值准备5套图片资源: 
    drawable:主要放置xml配置文件或者对分辨率要求较低的图片 
    drawalbe-ldpi:低分辨率的图片,QVGA (240x320) 
    drawable-mdpi:中等分辨率的图片,HVGA (320x480) 
    drawable-hdpi:高分辨率的图片,WVGA (480x800),FWVGA (480x854) 
    drawable-xhdpi:至少960dp x 720dp 
Android有个自动匹配机制去选择对应的布局和图片资源。 
  系统会根据机器的分辨率来分别到这几个文件夹里面去找对应的图片。 
  在开发程序时为了兼容不同平台不同屏幕,建议各自文件夹根据需求均存放不同版本图片。 

3.AndroidManifest.xml 配置 
android1.6和更高,Google为了方便开发者对于各种分辨率机型的移植而增加了自动适配的功能 
          <supports-screens 
           android:largeScreens="true"  
            android:normalScreens="true" 
            android:smallScreens="true"  
            android:anyDensity="true"/> 

3.1是否支持多种不同密度的屏幕 
android:anyDensity=["true" | "false"] 
如果android:anyDensity="true" 指应用程序支持不同密度,会根据屏幕的分辨率自动去匹配。 
如果android:anyDensity="false" 应用程序支持不同密度,系统自动缩放图片尺寸和这个图片的坐标。具体解释一下系统是如何自动缩放资源的。 

例如我们在hdpi,mdpi,ldpi文件夹下拥有同一种资源,那么应用也不会自动地去相应文件夹下寻找资源,这种情况都是出现在高密度,以及低密度的手机上,比如说一部240×320像素的手机, 如果设置android:anyDensity="false"Android系统会将240 x 320(低密度)转换为 320×480(中密度),这样的话,应用就会在小密度手机上加载mdpi文件中的资源。 

3.2是否支持大屏幕 
android:largeScreens=["true" | "false"] 
如果在声明不支持的大屏幕,而这个屏幕尺寸是larger的话,系统使用尺寸为("normal")和密度为("medium)显示, 
不过会出现一层黑色的背景。 

3.3是否支持小屏幕 
android:smallScreens=["true" | "false"] 
如果在声明不支持的小屏幕,而当前屏幕尺寸是smaller的话,系统也使用尺寸为("normal")和密度为("medium)显示 
如果应用程序能在小屏幕上正确缩放(最低是small尺寸或最小宽度320dp),那就不需要用到本属性。否则,就应该为最小屏幕宽度标识符设置本属性 
来匹配应用程序所需的最小尺寸。 

3.1是否支持多种不同密度的屏幕 
android:anyDensity=["true" | "false"] 
如果android:anyDensity="true" 指应用程序支持不同密度,会根据屏幕的分辨率自动去匹配。 
如果android:anyDensity="false" 应用程序支持不同密度,系统自动缩放图片尺寸和这个图片的坐标。具体解释一下系统是如何自动缩放资源的。 

例如我们在hdpi,mdpi,ldpi文件夹下拥有同一种资源,那么应用也不会自动地去相应文件夹下寻找资源,这种情况都是出现在高密度,以及低密度的手机上,比如说一部240×320像素的手机, 
如果设置android:anyDensity="false"Android系统会将240 x 320(低密度)转换为 320×480(中密度),这样的话,应用就会在小密度手机上加载mdpi文件中的资源。 

3.2是否支持大屏幕 
android:largeScreens=["true" | "false"] 
如果在声明不支持的大屏幕,而这个屏幕尺寸是larger的话,系统使用尺寸为("normal")和密度为("medium)显示, 
不过会出现一层黑色的背景。 

3.3是否支持小屏幕 
android:smallScreens=["true" | "false"] 
如果在声明不支持的小屏幕,而当前屏幕尺寸是smaller的话,系统也使用尺寸为("normal")和密度为("medium)显示 
如果应用程序能在小屏幕上正确缩放(最低是small尺寸或最小宽度320dp),那就不需要用到本属性。否则,就应该为最小屏幕宽度标识符设置本属性 
来匹配应用程序所需的最小尺寸。 

 


《实践》

具体解决方案:

1.细说layout_weight

    目前最为推荐的Android多屏幕自适应解决方案。

    该属性的作用是决定控件在其父布局中的显示权重,一般用于线性布局中。其值越小,则对应的layout_width或layout_height的优先级就越高,一般横向布局中,决定的是layout_width的优先级;纵向布局中,决定的是layout_height的优先级。

    传统的layout_weight使用方法是将当前控件的layout_width和layout_height都设置成fill_parent,这样就可以把控件的显示比例完全交给layout_weight;这样使用的话,就出现了layout_weight越小,显示比例越大的情况。不过对于2个控件还好,如果控件过多,且显示比例也不相同的时候,控制起来就比较麻烦了,毕竟反比不是那么好确定的。

于是就有了现在最为流行的0px设值法。看似让人难以理解的layout_height=0px的写法,结合layout_weight,却可以使控件成正比例显示,轻松解决了当前Android开发最为头疼的碎片化问题之一。

先看下面的styles(style_layout.xml)

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

可以看到,layout_widthlayout_height两个属性被我封装成了4style

 

  根据实际布局情况,选用当中的一种,不需要自己设置,Demo的布局如下(weight_layout.xml

<?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的界面(后面的图也如此),

可以看出显示比例和代码中完全一致,我就不多说了,大家对照下就能看出来了。


2.自定义尺寸法

  这个是我自己想出来的方法,可能是个比较笨的方法,所以没有多少人提过用这种方法解决自适应的问题。虽然这个方法缺点也很多,但有时候也是个不错的方法。

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

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

这个是values-480x320文件夹下dimens_height.xml文件中的代码,我把整个高度分成了80等分,这是因为大部分屏幕的宽度或高度都是80的整数倍(个别特殊的除外),不同的等分在不同的分辨率中设定不同的尺寸值。

 由于每一套界面都要写一套,所以有些同学可能觉着不太好,不过这个写起来比较简单,而且以后也不用改,所以有时候也可以考虑用一下!

 再看我Demo的布局代码(dimen_layout.xml

<?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个方法有时候用起来反而还要方便一些)


3、多布局

  做为最后的方法,也是最后一个才会考虑的方法,那就是为不同的尺寸界面单独写布局。不到万不得已不要用这个方法,相信不少人和我一样都被逼着用过这个方法吧。需要说明的是,横竖屏切换使用不同布局也是用这个方法解决的;代码我就不上了,给大家看两张图吧,一个是同1个布局的,一个是写了多布局的,大家一看就明白了

补充一下,写多个布局的时候,配置文件一定要加上这段配置代码,不然有时可能会出问题

<supports-screens 
android:largeScreens="true"
android:normalScreens="true" 
android:anyDensity="true" />

布局的适配:

在第1点中我们已经解决了资源图片的适配问题,但是图片的大小不同势必会造成布局的差异,如果使用一个统一的布局文件,可能会造成资源图片无法完整显示在屏幕内,所以对于这种问题,解决方法是建立不同的layout文件夹,对于不同屏幕的分辨率,书写其专用的.xml文件。

多个layout文件夹在命名上有相应规则,以分辨率480x854为例,需要建立480x854像素的layout文件夹,命名:layout-854x480,有两点需要注意:大数(854)必须在前,否则会报错;②两个数字之间的符号是小写英文字母“x”,不是乘号。系统会根据机器的分辨率来分别到这几个文件夹里面去找对应的布局文件。


如果你建立的layout文件夹的分辨率是你的eclipse加载的android工具所支持的分辨率,那么在可视化的布局界面中会自动给出屏幕分辨率、屏幕尺寸(红色箭头处)等参数,值得注意的是,如果红色箭头处是“Custom…”,那么你就要人为对其进行修改,选择合适的屏幕尺寸。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值