Android手机适配

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-1280x720value-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 dppx的转换关系1dp = 1.5px,则160dp = 240px,当前控件宽度应该位于屏幕中间位置。

注意:设置了个性化分辨率手机的属性后,需要也设置一个相同的默认属性,不然程序会挂的。

3layout布局适配

(不同手机(不同的像素密度),会去加载不同资源文件下的布局文件)

创建多个layout(如:layout-1280x720layout-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>

这是官方给出的解决方案,这需要对每种分辨率的手机都要写一个布局文件,虽然看似解决了分辨率的问题,

但是如果其中一处或多处有修改了,就要每个布局文件都要做出修改,这样就造成很大的麻烦。

4java代码适配

(获取屏幕的宽高,控件的宽高按屏幕的比例去做设置)

当前的宽高需要优先设置在控件的父布局上,让后再作用在子控件身上

 

布局文件

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

activityoncreate核心代码:

<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图,可以自动的依据图片上面显示的内容被拉伸和收缩。其中在编辑的时候,灰色区域是被拉伸的,上下两个点控制水平方向的拉伸,左右两点控制垂直方向的拉伸









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值