关于android的屏幕适配
2016-03-11 16:32
615 查看
前言:
之前查阅了一些文档,比如:
http://blog.csdn.net/zhaokaiqiang1992/article/details/45419023
虽然是很详细,但是感觉好复杂,所以只是略微了解了一些概念性的东西
之后又看到了这篇博客:
http://blog.csdn.net/lmj623565791/article/details/46767825
虽然是在Google的基础上做了很大改善,我也期望能参考这个库,但是再导入库时,始终无法解决,
无奈之下,我就选择了直接使用Google的API,其用法总结如下:
正文:
1>导入API所需的库(仅需以下一步)
在build.gradle(Module:app)的
[/code]
[/code]
之前查阅了一些文档,比如:
http://blog.csdn.net/zhaokaiqiang1992/article/details/45419023
虽然是很详细,但是感觉好复杂,所以只是略微了解了一些概念性的东西
之后又看到了这篇博客:
http://blog.csdn.net/lmj623565791/article/details/46767825
虽然是在Google的基础上做了很大改善,我也期望能参考这个库,但是再导入库时,始终无法解决,
无奈之下,我就选择了直接使用Google的API,其用法总结如下:
正文:
1>导入API所需的库(仅需以下一步)
在build.gradle(Module:app)的
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' compile project(':PhysicaloidLibrary') }
[code]
添加一行:[code]compile 'com.android.support:percent:23.0.0'
添加后的结果为:
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' compile project(':PhysicaloidLibrary') compile 'com.android.support:percent:23.0.0' }
[/code]
2>用法
示例如下:
[code]<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.dai.app2.ExtrudeActivity" tools:showIn="@layout/activity_extrude"> <!-- PercentRelativeLayout: 其比例是根据子布局的尺寸和margin来计算的 控件的宽是依据子布局的宽,亦即水平方向的子布局的长度 控件的高是依据子布局的高,亦即竖直方向的子布局的长度 参考链接: https://developer.android.com/intl/zh-cn/reference/ android/support/percent/package-summary.html 注:屏幕尺寸的宽高是根据屏幕分辨率的属性来的,不是 通过测试程序得出来的值; 例如:我通过程序测试出来的屏幕的宽高为1024*552 而在供应商那边得到的数据是:1024*600 所以在percent中,我应该选择600,而不是552; --> <android.support.percent.PercentRelativeLayout android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"> <Button android:layout_width="0dp" android:layout_height="0dp" app:layout_widthPercent="53.515625%" app:layout_heightPercent="21.666667%" android:id="@+id/btnZUpExtrude" android:background="@drawable/extrudezupdis" android:enabled="false" android:layout_centerHorizontal="true" app:layout_marginTopPercent="23.8%" /> <Button android:layout_width="0dp" android:layout_height="0dp" app:layout_widthPercent="53.515625%" app:layout_heightPercent="21.666667%" android:id="@+id/btnZDownExtrude" android:background="@drawable/extrudezdowndis" android:enabled="false" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" app:layout_marginBottomPercent="23.8%" /> <Button android:layout_width="0dp" android:layout_height="0dp" app:layout_widthPercent="20.3125%" app:layout_heightPercent="26.811594%" android:id="@+id/btnXCenterExtrude" android:background="@drawable/extrudexdis" android:layout_centerVertical="true" app:layout_marginLeftPercent="18.75%" /> <Button android:layout_width="0dp" android:layout_height="0dp" app:layout_widthPercent="20.3125%" app:layout_heightPercent="26.811594%" android:id="@+id/btnYCenterExtrude" android:background="@drawable/extrudeydis" android:layout_alignParentRight="true" android:layout_centerVertical="true" app:layout_marginRightPercent="18.554688%" /> <Button android:layout_width="0dp" android:layout_height="0dp" app:layout_widthPercent="19.53125%" app:layout_heightPercent="16.66666667%" android:id="@+id/btnZZeroExtrude" android:background="@drawable/extrudez0dis" android:onClick="zZero" android:layout_centerInParent="true" /> </android.support.percent.PercentRelativeLayout> <android.support.percent.PercentRelativeLayout android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"> <Button android:layout_width="0dp" android:layout_height="0dp" app:layout_widthPercent="51.757812%" app:layout_heightPercent="20.66667%" android:id="@+id/btnReturnWireExtrude" android:background="@drawable/extrudereturnwire" android:layout_centerHorizontal="true" app:layout_marginTopPercent="24.3%" /> <Button android:layout_width="0dp" android:layout_height="0dp" app:layout_widthPercent="51.757812%" app:layout_heightPercent="20.66667%" android:id="@+id/btnExtrudeExtrude" android:background="@drawable/extrude" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" app:layout_marginBottomPercent="24%" /> <Button android:layout_width="0dp" android:layout_height="0dp" app:layout_widthPercent="20.3125%" app:layout_heightPercent="24.666667%" android:id="@+id/btnCoolDownExtrude" android:background="@drawable/extrudecooldown" android:layout_centerVertical="true" app:layout_marginLeftPercent="18.945312%" /> <Button android:layout_width="0dp" android:layout_height="0dp" app:layout_widthPercent="20.3125%" app:layout_heightPercent="24.666667%" android:id="@+id/btnHeatUpExtrude" android:background="@drawable/extrudeheatingup" android:layout_alignParentRight="true" android:layout_centerVertical="true" app:layout_marginRightPercent="18.945312%" /> <Button android:layout_width="0dp" android:layout_height="0dp" app:layout_widthPercent="17.578125%" app:layout_heightPercent="15%" android:id="@+id/btnTemperatureExtrude" android:background="@drawable/extrudetemperature" android:text="Temperature" android:textColor="@android:color/white" android:layout_centerInParent="true" /> </android.support.percent.PercentRelativeLayout> </LinearLayout>
[/code]
其效果图如下:
这样,当我换一种机型的时候,其会根据屏幕的大小而自动调整;若我在上述代码中,控件的宽高直接用具体的dp数值指定,
那么,当换一种尺寸的屏幕时,其效果图就会变形,用 % 就很好的解决了这个问题;
相关文章推荐
- android只title背景色随着滚动渐变颜色
- Android悬浮窗的实现
- Android RecyclerView的使用
- metadata
- Android studio导入问题
- Android 开发中,有哪些坑需要注意?
- 8.1.3 Android中的13种Drawable小结 Part 3
- Android设置按钮为透明
- 8.1.2 Android中的13种Drawable小结 Part 2
- 8.1.1 Android中的13种Drawable小结 Part 1
- 笔记:Android源码和NDK目录及头文件
- ListView点击Item更换显示内容图片
- Android硬件加速
- Android 获取本地图片
- 论Android Studio 中 mipmaps 与 drawable 的区别?
- android屏幕的适配方法
- android listview 在添加header时候 OnItemClickListener position 索引不正确
- Android布局优化之ViewStub、include、merge使用与源码分析
- Android DatagramSocket error message: EADDRINUSE (Address already in use)
- Android ps命令执行后的各项参数含义