Android自适应不同屏幕几种方法
16lz
2021-01-23
由于Android设备的屏幕尺寸、分辨率差别非常大,如果希望我们的应用能够在不同屏幕尺寸或分辨率的Android设备上运行,即更换Android设备后界面和字体不会因此变得混乱,则需要考虑屏幕的自适应性问题。相关概念: (1)屏幕尺寸(Screen size):即指屏幕的对角线长度,屏幕尺寸可分为small(小屏幕)、normal(中等屏幕)、large(大屏幕)、xlarge(超大屏幕); (2)分辨率(dp):即整个屏幕有多少个像素点组成,可分为ldpi(低分辨率)、mdpi(中等分辨率)、hdpi(高分辨率)、xhdpi(超高分辨率); >xlarge屏幕尺寸分辨率至少需要960dp*720dp >large屏幕尺寸分辨率至少需要640dp*480dp >normal屏幕尺寸分辨率至少需要470dp*320dp >small屏幕尺寸分辨率至少需要426dp*320dp (3)像素密度(dpi):每英寸屏幕含有的像素点个数 (android 也按照像素密度分了四个等级,分别是low, medium, high, 和 extra high); (4)方 向(Orientation):分水平和垂直,如果应用做的好的话,这两个方向都要考虑; android 对不同尺寸不同像素密度等级划分: 一、使用layout_weight属性方法 目前最为推荐的Android多屏幕自适应解决方案。 该属性的作用是决定控件在其父布局中的显示权重,一般用于线性布局中。layout_weight属性值越小,则对应的layout_width或layout_height的优先级就越高,一般横向布局中,决定的是layout_width的优先级;纵向布局中,决定的是layout_height的优先级。目前有两种方法:(1) 传统使用方法 :将layout_width和layout_height设置为fill_parent,通过 layout_weight属性控制控件的显示比例,当layout_weight越小,控件显示比例就越大。但是,如果布局中控件较多且显示比例不相同时,传统使用layout_weight属性的方法将比较麻烦。(2) 0px设值法:即使layout_width="0dp" 或layout_height="0dp",再结合layout_weight属性正比例控制控件的显示,该方法有效解决了当前Android开发中碎片化问题之一。 源码举例: 1.res/values/style_layout.xml 功能:将控件的layout_width和layout_height两个属性封装成4个style <?xml version="1.0" encoding="utf-8"?> <resources> <!-- 全屏幕拉伸--> <style name="layout_full"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">fill_parent</item> </style> <!-- 固定自身大小--> <style name="layout_wrap"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> </style> <!-- 横向分布--> <style name="layout_horizontal" parent="layout_full"> <item name="android:layout_width">0px</item> </style> <!-- 纵向分布--> <style name="layout_vertical" parent="layout_full"> <item name="android:layout_height">0px</item> </style> </resources> 2.res/layout/weight_layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/layout_full" android:orientation="vertical"> <LinearLayout style="@style/layout_vertical" android:layout_weight="1" android:orientation="horizontal"> <View style="@style/layout_horizontal" android:background="#aa0000" android:layout_weight="1"/> <View style="@style/layout_horizontal" android:background="#00aa00" android:layout_weight="4"/> <View style="@style/layout_horizontal" android:background="#0000aa" android:layout_weight="3"/> <View style="@style/layout_horizontal" android:background="#aaaaaa" android:layout_weight="2"/> </LinearLayout> <LinearLayout style="@style/layout_vertical" android:layout_weight="2" android:orientation="vertical"> <View style="@style/layout_vertical" android:background="#ffffff" android:layout_weight="4"/> <View style="@style/layout_vertical" android:background="#aa0000" android:layout_weight="3"/> <View style="@style/layout_vertical" android:background="#00aa00" android:layout_weight="2"/> <View style="@style/layout_vertical" android:background="#0000aa" android:layout_weight="1"/> </LinearLayout> </LinearLayout> 3.效果演示 如图所示,分别为480*800界面和320*480界面。
二、自定义尺寸法 所谓自定义尺寸法,即为每一种屏幕界面定义一套界面尺寸大小。
1.res/values-480x320/dimen_height.xml 功能:定义两套应用于不同界面的尺寸 dimen name="height_1_80">6dp</dimen> <dimen name="height_3_80">18dp</dimen> <dimen name="height_5_80">30dp</dimen> <dimen name="height_7_80">42dp</dimen> <dimen name="height_9_80">54dp</dimen> <dimen name="height_11_80">66dp</dimen> <dimen name="height_13_80">78px</dimen> <dimen name="height_15_80">90px</dimen> <dimen name="height_17_80">102px</dimen> ........... </resources> 2.dimen_layout.xml 功能:根据不同分辨率屏幕,使用不同的尺寸 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <View android:layout_width="@dimen/width_76_80" android:layout_height="@dimen/height_10_80" android:background="#ffcccc" android:layout_margin="@dimen/width_2_80"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <View android:layout_width="@dimen/width_30_80" android:layout_height="@dimen/height_50_80" android:background="#ccccff" android:layout_margin="@dimen/height_5_80"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Button android:layout_width="@dimen/width_30_80" android:layout_height="@dimen/height_5_80" android:background="#ccffcc" android:layout_marginBottom="@dimen/height_1_80" android:text="5"/> <Button android:layout_width="@dimen/width_30_80" android:layout_height="@dimen/height_10_80" android:background="#ccffcc" android:layout_marginBottom="@dimen/height_1_80" android:text="10"/> <Button android:layout_width="@dimen/width_30_80" android:layout_height="@dimen/height_15_80" android:background="#ccffcc" android:layout_marginBottom="@dimen/height_1_80" android:text="15"/> <Button android:layout_width="@dimen/width_30_80" android:layout_height="@dimen/height_20_80" android:background="#ccffcc" android:text="20"/> </LinearLayout> </LinearLayout> </LinearLayout> 3.效果演示
三、多布局 所谓多布局,即为不同尺寸屏幕设计单独的布局文件。为了提供自适应不同屏幕的资源,我们可以为不同屏幕尺寸、不同屏幕分辨率提供相应的布局资源、Drawable资源。需要注意的是,使用多布局方法,须在清单文件中添加如下代码,否则,会出现异常。
1.如何支持多个屏幕 (1)在程序中显示声明你的应用程序支持屏幕尺寸的清单(AndroidMnifest.xml)
参考: http://www.eoeandroid.com/thread-173973-1-1.html http://blog.csdn.net/lonely_fireworks/article/details/7233575 http://blog.csdn.net/kangyaping/article/details/7400280
二、自定义尺寸法 所谓自定义尺寸法,即为每一种屏幕界面定义一套界面尺寸大小。
1.res/values-480x320/dimen_height.xml 功能:定义两套应用于不同界面的尺寸 dimen name="height_1_80">6dp</dimen> <dimen name="height_3_80">18dp</dimen> <dimen name="height_5_80">30dp</dimen> <dimen name="height_7_80">42dp</dimen> <dimen name="height_9_80">54dp</dimen> <dimen name="height_11_80">66dp</dimen> <dimen name="height_13_80">78px</dimen> <dimen name="height_15_80">90px</dimen> <dimen name="height_17_80">102px</dimen> ........... </resources> 2.dimen_layout.xml 功能:根据不同分辨率屏幕,使用不同的尺寸 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <View android:layout_width="@dimen/width_76_80" android:layout_height="@dimen/height_10_80" android:background="#ffcccc" android:layout_margin="@dimen/width_2_80"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <View android:layout_width="@dimen/width_30_80" android:layout_height="@dimen/height_50_80" android:background="#ccccff" android:layout_margin="@dimen/height_5_80"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Button android:layout_width="@dimen/width_30_80" android:layout_height="@dimen/height_5_80" android:background="#ccffcc" android:layout_marginBottom="@dimen/height_1_80" android:text="5"/> <Button android:layout_width="@dimen/width_30_80" android:layout_height="@dimen/height_10_80" android:background="#ccffcc" android:layout_marginBottom="@dimen/height_1_80" android:text="10"/> <Button android:layout_width="@dimen/width_30_80" android:layout_height="@dimen/height_15_80" android:background="#ccffcc" android:layout_marginBottom="@dimen/height_1_80" android:text="15"/> <Button android:layout_width="@dimen/width_30_80" android:layout_height="@dimen/height_20_80" android:background="#ccffcc" android:text="20"/> </LinearLayout> </LinearLayout> </LinearLayout> 3.效果演示
三、多布局 所谓多布局,即为不同尺寸屏幕设计单独的布局文件。为了提供自适应不同屏幕的资源,我们可以为不同屏幕尺寸、不同屏幕分辨率提供相应的布局资源、Drawable资源。需要注意的是,使用多布局方法,须在清单文件中添加如下代码,否则,会出现异常。
1.如何支持多个屏幕 (1)在程序中显示声明你的应用程序支持屏幕尺寸的清单(AndroidMnifest.xml)
- <supports-screens
- android:resizeable=[“true”|”false”]
- android:smallScreens=[“true”|”false”]
- android:normalScreens=[“true”|”false”]
- android:largeScreens=[“true”|”false”]
- android:xlargeScreens=[“true”|”false”]
- android:anyDensity=[“true”|”false”]/>
参考: http://www.eoeandroid.com/thread-173973-1-1.html http://blog.csdn.net/lonely_fireworks/article/details/7233575 http://blog.csdn.net/kangyaping/article/details/7400280
更多相关文章
- Android布局文件中控件的高度与宽度属性设置
- [android]笔记4-线性布局
- Android屏幕完美适配最全攻略(最权威的官方适配指导)
- Android的Activity屏幕切换动画(二)-左右滑动深入与实战
- [置顶] Android的Activity屏幕切换动画(二)-左右滑动深入与实战
- Android软键盘-弹起时布局向上拉-多表单填写界面
- [转载]Android布局文件中命名空间的解析
- TableLayout(表格布局)
- Android 布局 之 TableLayout,RelativeLayout,AbsoluteLayout,Fram