android屏幕适配
来源:互联网 发布:vb skinsharp 编辑:程序博客网 时间:2024/06/16 09:59
大家好,接下来由我给大家分想一个有关android屏幕适配方面的问题。关于android屏幕适配呢,google想得比我们更深更远,google提供了一系列能够适配各种设备的适配方法,我们需要了解一些必要适配方法,以下适配方法是个人概括,详细知识请看google官方API。
一、google提供单位
google推荐使用单位dp(device independent pixels)设备独立像素,功能是实现在不同屏幕尺寸和不同屏幕分辨率上的1dp长度近似相等。
单纯使用dp的问题:如上图例子所示,同样400dp在7寸上能完美显示,但是在小屏幕5寸上则显示不完整。
字体单位则使用sp
二、Google提供适配方法:
google已经提供了各种限定符,资源文件添加不同的限定符,系统会根据运行时的状态加载不同的资源文件。如语言-地区限定符,以下介绍项目常用的限定符
分辨率
-1920x1080 -1280x720等,例如新建目录res/values-1920x1080/dimens.xml,屏幕为1920x1080的设备下就会加载该目录下的资源。
像素密度
-mdpi -hdpi -xhdpi -xxhdpi 等,例如-xxhdpi 在高屏幕像素密度 bitmap后添加,手机是高像素密度的加载该目录下的资源,实现高屏幕密度加载高质量图片。需要注意的是如果该目录下没有图片资源,而-mdpi下有相同名字的图片资源,系统默认会先把-mdp的图片资源经过图片放大处理后再使用。
屏幕尺寸
例如-sw600dp,新建目录res/values-sw600dp /dimens.xml,sw是samll widh的简称,屏幕宽度大于600dp的设备加载该目录下资源。
屏幕方向
-port -land等,例如-land, 新建目录res/values-land /dimens.xml,在屏幕是横屏状态时调用此目录下资源。
三、Google提供百分比适配方法
PercentRelativeLayout PercentFrameLayout布局, 定义的长度不是一个dp一样的固定值,而是一个比例,如下是一个在不同尺寸不同分辨率下,定义一个50%的长度是屏幕长或宽的50%。
使用该布局需要导入android.support.percent.PercentRelativeLayout的包,详细介绍查看项目
compile 'com.android.support:percent:25.2.0'
使用时,如下:android:layout_height=”0dp ”//此项必须折这为0dp app:layout_heightPercent=”27%”//此项填比例27%,表示屏幕高度的27%
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/attr_grid" android:layout_width="match_parent" android:layout_height="0dp" app:layout_heightPercent="27%" android:padding="5dp"> </android.support.v7.widget.RecyclerView></android.support.percent.PercentRelativeLayout>
宽度类似,layout_width=“0dp”//此项必须为0dp app:layout_widthtPercent=”27%”,此时是屏幕宽度的27%
所以,此适配方法无法构造出一个正方形的控件。
四、针对本项目的其他适配方法
百分比适配方法二
项目中同时提供了第二中实现百分比适配的方案,比较与Google提供的百分比方案具有以下优点:
1、使用方法简单,
2、能构造出正方形,
3、可以使用限定符加载不同的比例的优点。
缺点:
基于分辨率限定符实现,不能完全适配所有设备。
准备:新建多个适配文件,详细原理请查看博客
使用方法:
例如:layout/layer_attr_fragment.xml布局中,使用了宽度android:layout_width=”@dimen/dialog_width“,
在values/dimens.xml中,定义了宽度dilog_width,
<dimen name="dialog_width">@dimen/x260</dimen>
在values-sw600dp/dimens.xml,也定义了宽度dilog_width,
<dimen name="dialog_width">@dimen/x200</dimen>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="@dimen/dialog_width" android:layout_height="wrap_content" android:baselineAligned="false" android:orientation="horizontal" android:padding="6dp"> <ListView android:layout_weight="1" android:id="@+id/listViewAttrHeader" android:layout_width="0dp" android:layout_height="match_parent"/> <FrameLayout android:id="@+id/frameLayoutAttrContainer" android:layout_width="0dp" android:layout_height="@dimen/dialog_height" android:layout_marginLeft="5dp" android:layout_marginStart="5dp" android:layout_weight="3"> </FrameLayout></LinearLayout>
需要记住的是这儿有一个基准,320宽和480高,如x260实际上代表了该控件的宽度是260/320的百分比,如y240实际上的长度=设备的高度*240/480,也就是设备高度的50%。
单面板和双面板
根据不同尺寸加载不同布局文件,实现在小尺寸屏幕上显示单面板,在大尺寸屏幕上显示双面板。
例如实现在平板上双面板,手机上单面板显示图层和属性的fragment:
1. layout 下新建两套布局:
layout/main_activity_onepane.xml
layout/main_activity_twopane.xml
2 .values/layouts.xml 下引用单面板的布局:
<item name="main_activity" type="layout">@layout/main_activity_onepane</item><bool name="has_two_panes" >false</bool>
3. values-sw600dp/layouts.xml引用双面板 的布局:
<item name="main_activity" type="layout">@layout/main_activity_twopane</item><bool name="has_two_panes" >true</bool>
4.然后activity加载R.layout.main_activity布局,这就实现了同一界面下600dp以上宽的加载main_activity_twopane.xml,600dp款以下的设备加载main_activity_onepane.xml。
@Overridepublic int getViewResId() { return R.layout.main_activity;}
5.最后动态判断设备宽度,在逻辑代码中,根据显示需求为布局动态加载不同的fragment。
if (PaneUtil.hasTwoPanes(getContext())) { //todo同时为两个面板加载两个fragment} else { //todo 为一个面板加载第一个需要显示的fragment,需要加载第二个fragment时,再替换第一个fragment。}
6 . 本项目中使用PaneUtil. hasTwoPanes()判断是否使用双面板,然后加载不同布局。
public static boolean hasTwoPanes(Context context) { return context.getResources().getBoolean(R.bool.has_two_panes);}
五、总结
根据Google提供的适配方法,我们可以根据项目需求灵活使用,选用最佳适配方法。
1我们在项目中定义控件高度或宽带时,尽量使用wrap_content或match_parent.。
2如果定义一个可滑动的空间的高度,尽量使用百分比适配方法。
3如果有在平板上显示内容过多,例如布局宽度超过400dp才能显示完的,一定要考虑使用双面版,适配到手机时使用单面板显示。
4如果在平板上定义一个固定的值,如300dp的,如要在res/values-sw600dp下的demens.xml定义一个变量为300dp,然后需要考虑在手机加载的res/values/dimens.xml中,添加一个变量名相同但值稍小的,同时根据显示效果,需要在横屏模式下加载的res/values-land/dimens.xml中下添加一个相同的变量。
- 【Android】屏幕适配
- android屏幕适配
- android屏幕适配
- android 屏幕适配
- Android屏幕适配
- Android屏幕适配
- Android屏幕适配
- Android屏幕适配
- android 屏幕适配
- Android屏幕适配
- Android屏幕适配
- Android 屏幕适配
- android屏幕适配
- Android屏幕适配
- Android屏幕适配
- Android 屏幕适配
- android屏幕适配
- Android屏幕适配
- 运行时注解解析
- virtualenvwrapper创建独立的项目python环境
- jmeter内存溢出的解决方法
- Mybatis 如何 返回 List<String> 类型 或 List<Map<String,Object>>类型
- redis做缓存需要捕获异常
- android屏幕适配
- 线程的同步控制synchronized和lock的对比和区别
- 实例详解机器学习如何解决问题
- cocos2dx3.x《格斗》类游戏加载界面(一)
- mysql sql语句大全
- Mac_Git创建远程项目,同步更新
- centos启动Tomcat巨慢
- 实用的学习前端开发及推荐学习路线?
- JVM-类加载机制