Android自适应不同屏幕几种方法

    因为Android设备的屏幕尺寸、分辨率区别很大。假设希望我们的应用可以在不同屏幕尺寸或分辨率的Android设备上执行,即更换Android设备后界面和字体不会因此变得混乱。则须要考虑屏幕的自适应性问题。相关概念:
(1)屏幕尺寸(Screen size):即指屏幕的对角线长度,屏幕尺寸可分为small(小屏幕)、normal(中等屏幕)、large(大屏幕)、xlarge(超大屏幕);
(2)分辨率(dp):即整个屏幕有多少个像素点组成,可分为ldpi(低分辨率)、mdpi(中等分辨率)、hdpi(高分辨率)、xhdpi(超高分辨率);
    >xlarge屏幕尺寸分辨率至少须要960dp*720dp
    >large屏幕尺寸分辨率至少须要640dp*480dp
    >normal屏幕尺寸分辨率至少须要470dp*320dp
    >small屏幕尺寸分辨率至少须要426dp*320dp
(3)像素密度(dpi):每英寸屏幕含有的像素点个数 (android 也依照像素密度分了四个等级。各自是low, medium, high, 和 extra high);
(4)方向(Orientation) :分水平和垂直,假设应用做的好的话,这两个方向都要考虑;
    android 对不同尺寸不同像素密度等级划分:
 
一、使用layout_weight属性方法
    眼下最为推荐的Android多屏幕自适应解决方式。

该属性的作用决定控件在其父布局中的显示权重。一般用于线性布局中

layout_weight属性值越小。则相应的layout_width或layout_height的优先级就越高。一般横向布局中,决定的是layout_width的优先级。纵向布局中,决定的是layout_height的优先级。眼下有两种方法:(1)传统用法:将layout_width和layout_height设置为fill_parent,通过layout_weight属性控制控件的显示比例。当layout_weight越小,控件显示比例就越大。

可是,假设布局中控件较多且显示比例不同样时,传统使用layout_weight属性的方法将比較麻烦。(2)0px设值法:即使layout_width="0dp"或layout_height="0dp"。再结合layout_weight属性正比例控制控件的显示,该方法有效攻克了当前Android开发中碎片化问题之中的一个。

源代码举例:
1.res/values/style_layout.xml
功能:将控件的layout_width和layout_height两个属性封装成4个style
<?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>
2.res/layout/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>
3.效果演示
    如图所看到的,分别为480*800界面和320*480界面。

二、自己定义尺寸法
    所谓自己定义尺寸法,即为每一种屏幕界面定义一套界面尺寸大小。

1.res/values-480x320/dimen_height.xml
功能:定义两套应用于不同界面的尺寸
dimen name="height_1_80">6dp</dimen>     
<dimen name="height_3_80">18dp</dimen>   
<dimen name="height_5_80">30dp</dimen> 
<dimen name="height_7_80">42dp</dimen> 
<dimen name="height_9_80">54dp</dimen> 
<dimen name="height_11_80">66dp</dimen> 
<dimen name="height_13_80">78px</dimen>
<dimen name="height_15_80">90px</dimen> 
<dimen name="height_17_80">102px</dimen>
     ...........
</resources>
2.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>
3.效果演示

三、多布局
    所谓多布局,即为不同尺寸屏幕设计单独的布局文件。

为了提供自适应不同屏幕的资源,我们能够为不同屏幕尺寸、不同屏幕分辨率提供对应的布局资源、Drawable资源。须要注意的是,使用多布局方法。须在清单文件里加入例如以下代码,否则。会出现异常。


1.怎样支持多个屏幕
(1)在程序中显示声明你的应用程序支持屏幕尺寸的清单(AndroidMnifest.xml)
  1. <supports-screens   
  2.      android:resizeable=[“true”|”false”]  
  3.      android:smallScreens=[“true”|”false”]  
  4.      android:normalScreens=[“true”|”false”]  
  5.      android:largeScreens=[“true”|”false”]  
  6.      android:xlargeScreens=[“true”|”false”]  
  7.      android:anyDensity=[“true”|”false”]/>
2.文字自适应
(1)dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA (WVGA=800x480,HVGA=480x320, QVGA=320x240)推荐使用这个。不依赖像素。
(2)px: pixels(像素). 不同设备显示效果同样,一般我们HVGA代表320x480像素。这个用的比較多。

 

(3)pt: point,是一个标准的长度单位。1pt=1/72英寸。用于印刷业。很easy易用。 
(4)sp: scaled pixels(放大像素). 主要用于字体显示best for textsize。能够依据用户的字体大小首选项进行缩放 
    依据上面对单位的分析,使用sp为单位就能够实现自适应字体大小。

在res目录中创建一个目录,叫做values-320x240。当中320x240是你手机屏幕的分辨率,依据你手机屏幕的情况做不同的命名,比如values-800x480。在该目录下创建一个dimens.xml文件,定义各种字体的大小。那么系统就会自己主动依据你手机屏幕的分辨率去调用响应的目录。

 

        另外。值得提醒的是。记得在你默认的values文件下的dimens.xml文件里也要写上对应的字体大小,由于当系统无法认识你手机屏幕大小的时候。它会自己主动去找你默认文件里的东西,没有写的话程序会崩溃。
3.布局自适应
(1)依据不同屏幕尺寸。提供不同布局
    默认情况以下,android会自己主动调整应用程序的布局,可是大多数情况以下。依据广义尺寸。小,正常,大。更大去添加不同的布局资源。比方,假设须要对大小为large的屏幕提供支持,须要在res文件夹下新建一个文件夹layout-large/并提供layout。当然,也能够在res文件夹下建立layout-port和layout-land两个文件夹。里面分别放置竖屏和横屏两种布局文件,以适应对横屏竖屏自己主动切换。
layout :默认中等屏幕 
layout-small :小屏幕 
layout-large :大屏幕 
layout-xlarge :特大屏幕 
layout-land :横屏 
layout-port :竖屏
注:Android3.2以上推荐使用在layout、values文件夹加入-sw<N>dp。即屏幕尺寸至少宽N个dp才干使用该资源。

(2)提供不同的屏幕密度和不同的位图drawables 
    默认情况以下系统会自己主动调整和缩放位图,可是难免拉伸位图,为了保证你的位图是最好看的,依据广义密度。低。中型, 高。特高去加入不同的位图资源。比方,如需对密度为low的屏幕提供合适的图片,需新建目录drawable-ldpi/。

应尽量使用点9格式的图片,图片大小的确定:low:medium:high:xhigh比例为3:4:6:8。举例来说,对于中等密度(medium)的屏幕你的图片像素大小为48×48,那么低密度(low)屏幕的图片大小应为36×36,高(high)的为72×72,extra high为96×96

Android系统支持多配置资源文件,我们能够追加新的资源文件夹到你的Android项目中。
drawable-hdpi、drawable-mdpi、drawable-ldpi的差别:
>drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),FWVGA (480x854)  
>drawable-mdpi里面存放中等分辨率的图片,如HVGA (320x480)  
>drawable-ldpi里面存放低分辨率的图片,如QVGA (240x320) 

參考:
原文地址:https://www.cnblogs.com/lcchuguo/p/5135843.html