1、图片适配
(如果当前像素密度对应文件夹下,没有图片的时候,加载的高分辨率的图)
我们把这些可选的资源文件分别放在不同的目录,不同手机(不同的像素密度),会去加载不同资源文件下的图片。这一点类似于我们在国际化时不同语言strings的操作。
dp能够适配吗?不能适配。
通过查看官方文档可知,dp 与 px 有一个不同的比例关系
ldpi 1dp = 0.75px 320*240 160dp = 120px
mdpi 1dp = 1px 480*320 160dp = 160px
hdpi 1dp = 1.5px 800*480 160dp = 240px
xhdpi 1dp = 2px 1280*720 160dp = 320px<360px 180dp = 360px
前三个是刚好符合,到第四的时候,出现了明显的偏差。
友情提示:在公司里要与设计师妹妹打好关系哈。
2、dimens.xml适配
dimens.xml存在于工程资源(res)文件夹中不同values(如:value-1280x720、value-800x480)文件夹下,
可用于指定控件大小,不同像素密度手机加载不同values文件夹下的dimens.xml文件,使用方式如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<!-- 不同的手机加载不同的dp -->
<TextView
android:background="#987654"
android:layout_width="@dimen/width"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
模拟器(hdpi):加载dimens.xml资源文件,位于res/value-800x480文件夹下
<resources>
<dimen name="width">160dp</dimen>
</resources>
根据上述hdpi dp和px的转换关系1dp = 1.5px,则160dp = 240px,当前控件宽度应该位于屏幕中间位置。
注意:设置了个性化分辨率手机的属性后,需要也设置一个相同的默认属性,不然程序会挂的。
3、layout布局适配
(不同手机(不同的像素密度),会去加载不同资源文件下的布局文件)
创建多个layout(如:layout-1280x720、layout-800x480)文件夹用于存放不同像素密度手机所需布局文件。
模拟器(hdpi):加载activity_main.xml布局文件,位于res/layout-800x480文件夹下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="800*480手机会去加载的布局文件" />
</RelativeLayout>
这是官方给出的解决方案,这需要对每种分辨率的手机都要写一个布局文件,虽然看似解决了分辨率的问题,
但是如果其中一处或多处有修改了,就要每个布局文件都要做出修改,这样就造成很大的麻烦。
4、java代码适配
(获取屏幕的宽高,控件的宽高按屏幕的比例去做设置)
当前的宽高需要优先设置在控件的父布局上,让后再作用在子控件身上
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv"
android:background="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
activity中oncreate核心代码:
<span style="white-space:pre"> </span>TextView tv = (TextView) findViewById(R.id.tv);
//获取封装当前手机屏幕信息对象,用于存放宽高值
DisplayMetrics metrics = new DisplayMetrics();
//给当前屏幕设置宽高
getWindowManager().getDefaultDisplay().getMetrics(metrics);
//获取高度
Constant.srceenHeight = metrics.heightPixels;
//获取宽度
Constant.srceenWidth = metrics.widthPixels;
Log.i(tag, "Constant.srceenHeight = "+Constant.srceenHeight);
Log.i(tag, "Constant.srceenWidth = "+Constant.srceenWidth);
//宽高各占50%
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
(int)(Constant.srceenWidth*0.5+0.5),
(int)(Constant.srceenHeight*0.5+0.5));
tv.setLayoutParams(layoutParams);
5、权重适配
(剩余控件的一个分配的规则,线性布局中才可以使用权重)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >
<TextView
android:background="#000000"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<TextView
android:background="#123456"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
在屏幕适配的时候,通常我还会注意到几点:
1、首先会尽量使用线性布局,相对布局。
2、在指定宽高的时候,采用dip的单位,dp单位动态匹配
3、由于android代码中写的单位都是像素,所有需要通过工具类进行转化
4、尽量使用9-patch图,可以自动的依据图片上面显示的内容被拉伸和收缩。其中在编辑的时候,灰色区域是被拉伸的,上下两个点控制水平方向的拉伸,左右两点控制垂直方向的拉伸