Android下屏幕适配

  • 转自:http://www.it165.net/pro/html/201411/26030.html

  • Android下屏幕适配:

    适配:即当前应用在不同的手机上面显示相同的效果。适配前需要首先确定当前手机所属像素密度类型(如:xhdpi、hdpi、mdpi等),
    以下已华为G700、模拟器为例,计算其像素密度。

    案例一:

    手机型号:G700
    手机分辨率:1280*720 (注:手机两个直角边上分别放置了1280及720个像素点)
    手机尺寸大小:5英寸(手机对角线长度)

    假设a,b分别为两个直角边,c为斜边,由勾股定理可得出计算方式:sqrt(a*a+b*b)/c
    计算结果:sqrt(1280*1280+720*720)/5 ≈ 293.72dpi
    根据google官方文档说明得出,当前手机最接近320dpi,则将其归纳在xhdpi手机范围内,即1dp=2px;

    案例二:
    型号:模拟器
    手机分辨率:800*480(注:手机两个直角边上分别放置了800及480个像素点)
    尺寸大小:3.7英寸(手机斜边大小)

    计算结果:sqrt(800*800+480*480)/3.7 ≈ 252.15dpi
    根据google官方文档(图1-1)得出,当前手机接近240dpi,则将其归纳在hdpi手机范围内,即1dp=1.5px。

    参照以上方式可将市场上大多数手机划分为5个像素密度等级,分别为:
    ldpi: 120dpi,像素密度与dp转换关系为:1dp = 0.75px
    mdpi: 160dpi,像素密度与dp转换关系为:1dp = 1px
    hdpi: 240dpi,像素密度与dp转换关系为:1dp = 1.5px
    xhdpi: 320dpi,像素密度与dp转换关系为:1dp = 2px
    xxhdpi: 480dpi,像素密度与dp转换关系为:1dp = 3px

    (注:以下案例就当前两款手机进行屏幕适配测试)

    适配方式一:图片适配
    不同像素密度的手机加载工程资源文件(res)中不同资源图片,以上述两款手机为例。布局代码如下:

    01. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    03. android:layout_width="match_parent"
    04. android:layout_height="match_parent"
    05. tools:context=".MainActivity" >
    06. <ImageView
    07. android:layout_width="wrap_content"
    08. android:layout_height="wrap_content"
    09. android:background="@drawable/a" />
    10. </RelativeLayout>

    适配方式二:dimens.xml文件适配
    dimens.xml存在于工程资源(res)文件夹中不同values(如:value-1280x720、value-800x480)文件夹下,可用于指定控件大小,不同像素密度手机加载不同values文件夹下的dimens.xml文件,使用方式如下:

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. android:orientation="vertical"
    6. tools:context=".MainActivity" >

    <!-- 不同的手机加载不同的dp -->

    1. <TextView
    2. android:background="#987654"
    3. android:layout_width="@dimen/width"
    4. android:layout_height="wrap_content"
    5. android:text="@string/hello_world" />
    6. </LinearLayout>

    模拟器(hdpi):加载dimens.xml资源文件,位于res/value-800x480文件夹下

    1. <resources>
    2. <dimen name="width">160dp</dimen>
    3. </resources>


    根据上述hdpi dp和px的转换关系1dp = 1.5px,则160dp = 240px,当前控件宽度应该位于屏幕中间位置。 

    G700(xhdpi):加载dimens.xml资源文件,位于res/value-1280x720文件夹下

    1. <resources>
    2. <dimen name="width">180dp</dimen>
    3. </resources>


    根据上述xhdpi dp和px的转换关系1dp = 2px,则180dp = 360px,当前控件宽度应该位于屏幕中间位置。

    适配方式三:布局文件适配

    不同分辨率的手机,加载不同的布局文件已达到适配效果。创建多个layout(如:layout-1280x720、layout-800x480)文件夹用于存放不同像素密度手机所需布局文件。

    模拟器(hdpi):加载activity_main.xml布局文件,位于res/layout-800x480文件夹下:

    01. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    03. android:layout_width="match_parent"
    04. android:layout_height="match_parent"
    05. tools:context=".MainActivity" >
    06. <TextView
    07. android:layout_width="wrap_content"
    08. android:layout_height="wrap_content"
    09. android:text="800*480手机会去加载的布局文件" />
    10. </RelativeLayout>
    11.  
    12.  
    13. G700(xhdpi):加载activity_main.xml布局文件,位于res/layout-1280x720文件夹下:
    14.  
    15.  
    16. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    18. android:layout_width="match_parent"
    19. android:layout_height="match_parent"
    20. tools:context=".MainActivity" >
    21. <TextView
    22. android:layout_width="wrap_content"
    23. android:layout_height="wrap_content"
    24. android:text="1280*720手机会去加载的布局文件" />
    25. </RelativeLayout>

     适配方式四:java代码适配

    通过android相应api获取当前手机的宽高像素值,按比例分配屏幕中控件的宽高以达到适配效果。核心代码如下:
    布局文件:

    01. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    03. android:layout_width="match_parent"
    04. android:layout_height="match_parent"
    05. tools:context=".MainActivity" >
    06. <TextView
    07. android:id="@+id/tv"
    08. android:background="#000000"
    09. android:layout_width="wrap_content"
    10. android:layout_height="wrap_content"
    11. android:text="@string/hello_world" />
    12. </RelativeLayout>

    activity中onCreate核心代码:

    01. TextView tv = (TextView) findViewById(R.id.tv);
    02. //获取封装当前手机屏幕信息对象,用于存放宽高值
    03. DisplayMetrics metrics = new DisplayMetrics();
    04. //给当前屏幕设置宽高
    05. getWindowManager().getDefaultDisplay().getMetrics(metrics);
    06. //获取高度
    07. Constant.srceenHeight = metrics.heightPixels;
    08. //获取宽度
    09. Constant.srceenWidth = metrics.widthPixels;
    10. Log.i(tag, "Constant.srceenHeight = "+Constant.srceenHeight);
    11. Log.i(tag, "Constant.srceenWidth = "+Constant.srceenWidth);
    12.  
    13. //宽高各占50%
    14. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
    15. (int)(Constant.srceenWidth*0.5+0.5),
    16. (int)(Constant.srceenHeight*0.5+0.5));
    17. tv.setLayoutParams(layoutParams);


    适配方式五:权重适配
    通过android提供的(权重)剩余空间分配,已达到适配效果。显示界面加载布局文件如下:

    01. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    03. android:layout_width="match_parent"
    04. android:layout_height="match_parent"
    05. android:orientation="horizontal"
    06. tools:context=".MainActivity" >
    07. <TextView
    08. android:background="#000000"
    09. android:layout_width="0dp"
    10. android:layout_weight="1"
    11. android:layout_height="match_parent"/>
    12. <TextView
    13. android:background="#123456"
    14. android:layout_width="0dp"
    15. android:layout_weight="1"
    16. android:layout_height="match_parent"/>
    17. </LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值