关于Android适配,常用这几种就够了
来源:互联网 发布:java虚拟机占用内存 编辑:程序博客网 时间:2024/06/06 08:50
前言
Android 适配问题,让每个Android程序员头疼问题,听起来可能有些夸大,但是相信每个Android开发人员都碰到过棘手的适配问题。
关于Android适配的文章,各种论坛,眼花缭乱,有的很有营养,看完收货很大;有的则完全是复制粘贴,鱼目混珠.
我仅仅将自己在开发中用到的,感觉对适配有一定帮助的东西,写下来,希望能帮助到和我遇到过相同问题的人
正文
关于Android适配,google比我们想的更多,给我们提供了DP(DIP)-device independent pixels(设备独立像素).在一些简单布局中还是有用的,碰到一些复杂布局,DP完全满足不了需求,不然也不会让大家头疼的适配问题。
关于px/dp/ppi等概念,大家都很了解,我就不说这些,还不太明白的同学,可以看看
八一八那些px、pt、ppi、dpi、dp、sp之间的关系
详解Android开发中常用的 DPI / DP / SP
其实简单的适配,我们利用LinearLayout的layout_widget就可以完成比例的适配,但这远远不够,可以利用其它方法
这些文章已经说的很明了,所以废话不多说,直接讲讲适配问题
1、dimens
针对一些常见分辨率机型,设置dimens
dimens类似于drawable,drawable中我们设置
dimens则比drawable灵活,在values文件夹根据限定符去命名dimens
1.屏幕分辨率
例如1920x1080,1280x720,表示适配分辨率为1920x1080或者1280x720的设备
res/values-1920x1080/dimens.xml
2.屏幕像素密度
例如hdpi,适配屏幕像素密度值近似或等于hdpi(320dpi)的设备。
res/values-hdpi/dimens.xml
3.屏幕尺寸
例如 sw720dp,适配最低屏幕可用区域为720dp的设备,同理还有sw1080dp.
res/values-sw720dp/dimens.xml
4.屏幕方向
分为横向(land)和纵向(port),分别适配屏幕方向为横向或纵向的屏幕
res/values-land/dimens.xml
类似的限定符就不一一列举,就列出常见的限定符
但是这些限定符一般不是单独使用,而是组合使用,例如values-land-mdpi-800x480,values-xhdpi-port-1920×1080,
所以适配时,在values-land-mdpi-800x480中,定义 px100表示100px ,
<dimen name= "px100" >100px</ dimen>
则在values-hdpi-port-1280×720,px100则定义为mdpi的两倍
<dimen name= "px100" >200px</ dimen>
在布局中,就可以灵活使用dimens
<LinearLayoutandroid:layout_width="@dimen/px100"android:layout_height="@dimen/px100"android:orientation="vertical" />
针对不同分辨率,设置dimens实现适配,对市场上大部分主流分辨率,适配是没有问题的。
但是!!!Android设置碎片化实在是太严重,随便哪个厂家或者山寨厂商推出奇葩的分辨率,对我们都是莫大的伤害,然并卵,我们还是逃不掉,但是很多时候,丧心病狂的分辨率是我们无法预料的,我们不可能把所以可能出现的组合情况,全部设置dimens。那么,dimens也不能完全满足我们,就得想办法寻找新途径.
2.百分比适配
百分比适配,显而易见,通过设置百分比,实现控件适配,在Google没有提供百分比适配之前,我们使用的android:layout_weight,其实就是一种百分比,但是只能在LinearLayout中使用。
在Android 22开始,google提供了相当方便的百分比适配
使用很简单,依赖compile 'com.android.support:percent:24.0.0'
库
提供了PercentRelativeLayout、PercentFrameLayout两种布局,布局中,替换RelativeLayout、FrameLayout
那就简单看看PercentRelativeLayout的使用,PercentFrameLayout同理
<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="match_parent"> <TextView android:id="@+id/top_left" android:layout_width="0dp" android:layout_height="0dp" android:layout_alignParentTop="true" android:background="#ff44aacc" android:gravity="center" android:text="70%" app:layout_heightPercent="20%" app:layout_widthPercent="70%" /> <TextView android:id="@+id/top_right" android:layout_width="0dp" android:layout_height="0dp" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/top_left" android:background="#ffe40000" android:gravity="center" android:text="30%" app:layout_heightPercent="20%" app:layout_widthPercent="30%" /> <View android:id="@+id/bottom_left" android:layout_width="0dp" android:layout_height="0dp" android:layout_below="@+id/top_left" android:background="#ff00ff" app:layout_heightPercent="80%" app:layout_widthPercent="30%" /> <View android:id="@+id/bottom_right_top" android:layout_width="0dp" android:layout_height="0dp" android:layout_below="@+id/top_left" android:layout_toRightOf="@+id/bottom_left" android:background="#ff00ff22" app:layout_heightPercent="40%" app:layout_widthPercent="70%" /> <View android:id="@+id/bottom_right_bottom" android:layout_width="0dp" android:layout_height="0dp" android:layout_below="@+id/bottom_right_top" android:layout_toRightOf="@+id/bottom_left" android:background="#0000ff" app:layout_heightPercent="40%" app:layout_widthPercent="70%" /></android.support.percent.PercentRelativeLayout>
很简单的布局,效果也很直观
一共有layout_widthPercent、layout_heightPercent、 layout_marginPercent、layout_marginLeftPercent、 layout_marginTopPercent、layout_marginRightPercent、 layout_marginBottomPercent、layout_marginStartPercent、layout_marginEndPercent
属性提供使用
使用 layout_widthPercent、layout_heightPercent
,
不能删除默认的 layout_width、layout_height
,
而是设置 android:layout_width="0dp" android:layout_height="0dp"
3.代码适配
例如,要给一个图片动态设置尺寸,宽高都为屏幕宽度的77.8%
在Activity中,初始化 ivImgbackground;
CircleImageView ivImgbackground = findViewById(R.id.iv_imgbackground); /** * 动态设置图片大小 */ private void setImageSize() { final WindowManager manager = this.getWindowManager(); //得出屏幕宽度 DisplayMetrics outMetrics = new DisplayMetrics(); manager.getDefaultDisplay().getMetrics(outMetrics); widthPixels = outMetrics.widthPixels; //重新计算高度 layoutParams = ivImgbackground.getLayoutParams(); layoutParams.width = (int) (0.778 * widthPixels); layoutParams.height = (int) (0.778 * widthPixels); ivImgbackground.setLayoutParams(layoutParams); }
其实也是一种另类的百分比适配
- 关于Android适配,常用这几种就够了
- 关于 Android 适配,看完这篇文章就够了
- Android常用组件,太全了
- Android常用组件,太全了
- 收集了一些常用的Android代码
- 关于Android启动后都做了什么
- 十、Android关于蓝牙四个常用类
- 关于android常用的按下高亮的展示
- 关于Android较常用的组件介绍
- 关于Android 开发中的一些常用权限
- 关于Android 开发中的一些常用框架
- 关于学Android常用布局的感想
- Android关于时间日期相关常用方法
- Android常用的权限(关于Android uses-permission的资料)
- Android学习笔记-常用的一些源码,防止忘记了。。。
- Android学习笔记-常用的一些源码,防止忘记了。。。
- Android学习笔记-常用的一些源码,防止忘记了
- android 常用的一些源码,防止忘记了。。。
- ORACLE初学第二篇
- bzoj 4310: 跳蚤 (后缀数组+二分+ST表)
- Android中TextView属性全纪录
- Recyclerview使用体验(一)
- 关于SwipeRefreshLayout的刷新事件
- 关于Android适配,常用这几种就够了
- Android获取设备唯一标识
- - Unable to add window -- token null is not valid; is your activity running?
- Error:Execution failed for task ':app:transformNative_libsWithStripDebugSymbolForDebug'. > java.lang.NullPointerExcep
- win下的问题积累
- Android开发中--正则表达式的实用
- Android开发--更换字体
- KnockoutJS(一)
- Android开发—使用系统控件获取当前时间