.
3.到底要考虑布局,还是考虑图片,或者两者同时考虑由应用决定。我个人觉得,一般布局要考虑得多一些。如果应用有很多图片,而且对现实细腻程度要求很高,才考虑图片。
4.但是,所谓考虑多屏幕适应,并非要为没每种屏幕大小提供一个布局文件,也并非要为每种屏幕密度提供一套位图,按大的分类提供就可以了。(如果要那样做也没错,但将付出巨大的维护代价)
5.注意:实际开发中,为了适合多屏幕,又减少维护量。mdpi,hdpi内一般要放东西,ldpi则可放可不放。当要用到ldpi时,它从hdpi里去取并缩放。效果也不错。
6.QVGA HVGA WVGA
QVGA
中VGA的四分之一尺寸,亦即在液晶屏幕(LCD)上输出的分辨率是240×320像素。QVGA支持屏幕旋转,可以开发出相应的程序,以显示旋转90°、180°、270°屏幕位置。由HandEra公司发布。多用于手持/移动设备。
WVGA
即“Wide VGA” 。其分辩率为800×480象素。是扩大了VGA(640×480)的分辨率。应用于PDA和手机等,因为很多网页的宽度都是800,所以WVGA的屏幕会更加适和于浏览网页,可以说是未来手持设备的分辨率的大趋势。
HVGA 即“Half-size VGA”。是VGA的一半,分辨率为(480*320),(3:2宽高比),它是用于各种各样的PDA设备,首手机屏幕VGA QVGA HVGA WVGA区别,一般手机液晶屏幕都是TFT材质,VGA WVGA QVGA HVGE XGA只是表示屏幕分辨率只,是个代号和材质没关系。
分辨率对照表:
代号 分辨率 代号 分辨率
QVGA 320*240像素 WQVGA 400*240像素
HVGA 320*480像素 VGA 640*480像素
WVGA 800*480像素 XGA 1024*480像素
9.我在实际开发中,一般先按照WVGA标准,480*800,normalscreen,240dpi,3.5寸建立一个模拟器。布局文件目录先只建layout一个,图片也先放到drawable-hdpi下,然后在上面调试UI。觉得没问题了,才建立其他尺寸和密度的模拟器来看效果,只对需要调整的部分重新设计布局文件和图片,并放到合适的目录下。
10.
在AndroidManifest.xml中提供新的一个元素<supports-screens>用于支持多屏幕机制。
<supports-screens
/>
11.320X480 现在最主流的低端安卓机都是这个分辨率.
480X800 现在最主流的中高端安卓智能机的分辨率,包括 windows phone也是这个分辨率
480X854 这个是WFVGA,是480X800的加长版
1280X800 现在巨屏用了这个分辨率
1280X720 传说中的HD,也是现在各品牌主流旗舰机型的分辨率,而且屏幕都很大
12.在java代码中设置宽高度
public class Constant { public static int displayWidth; //屏幕宽度 public static int displayHeight; //屏幕高度 }
然后在第一个Activity启动的时候,获取这两个值
DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); Constant.displayWidth = displayMetrics.widthPixels; Constant.displayHeight = displayMetrics.heightPixels;
布局代码我们可以全都统一写成wrap-content,其实写成什么都无所谓,因为这个值只是暂时的
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffcccc" android:text="aaaaaaaa"/> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ccffcc" android:text="bbbbbbbbb"/> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ccccff" android:text="ccccccccc"/> <Button android:id="@+id/btn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffcc" android:text="dddddddddddddddddd"/> </LinearLayout>
最后我们在Activity的onCreate方法里这么做
// 第一个按钮,宽度100%,高度10% LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, (int) (Constant.displayHeight * 0.1f + 0.5f)); btn1.setLayoutParams(params); // 第二个按钮,宽度100%,高度30% LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, (int) (Constant.displayHeight * 0.3f + 0.5f)); btn2.setLayoutParams(params2); // 第三个按钮,宽度50%,高度20% LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams( (int) (Constant.displayWidth * 0.5f + 0.5f), (int) (Constant.displayHeight * 0.2f + 0.5f)); btn3.setLayoutParams(params3); // 第三个按钮,宽度70%,高度填满剩下的空间 LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams( (int) (Constant.displayWidth * 0.7f + 0.5f), LayoutParams.FILL_PARENT); btn4.setLayoutParams(params4)
定义了两套尺寸文件,我们可以看下其中一个文件
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="height_1_80">6px</dimen><dimen name="height_2_80">12px</dimen> <dimen name="height_3_80">18px</dimen><dimen name="height_4_80">24px</dimen> <dimen name="height_5_80">30px</dimen><dimen name="height_6_80">36px</dimen> <dimen name="height_7_80">42px</dimen><dimen name="height_8_80">48px</dimen> <dimen name="height_9_80">54px</dimen><dimen name="height_10_80">60px</dimen> <dimen name="height_11_80">66px</dimen><dimen name="height_12_80">72px</dimen> <dimen name="height_13_80">78px</dimen><dimen name="height_14_80">84px</dimen> <dimen name="height_15_80">90px</dimen><dimen name="height_16_80">96px</dimen> <dimen name="height_17_80">102px</dimen><dimen name="height_18_80">108px</dimen> <dimen name="height_19_80">114px</dimen><dimen name="height_20_80">120px</dimen> <dimen name="height_21_80">126px</dimen><dimen name="height_22_80">132px</dimen> <dimen name="height_23_80">138px</dimen><dimen name="height_24_80">144px</dimen> <dimen name="height_25_80">150px</dimen><dimen name="height_26_80">156px</dimen> <dimen name="height_27_80">162px</dimen><dimen name="height_28_80">168px</dimen> <dimen name="height_29_80">174px</dimen><dimen name="height_30_80">180px</dimen> <dimen name="height_31_80">186px</dimen><dimen name="height_32_80">192px</dimen> <dimen name="height_33_80">198px</dimen><dimen name="height_34_80">204px</dimen> <dimen name="height_35_80">210px</dimen><dimen name="height_36_80">216px</dimen> <dimen name="height_37_80">222px</dimen><dimen name="height_38_80">228px</dimen> <dimen name="height_39_80">234px</dimen><dimen name="height_40_80">240px</dimen> <dimen name="height_41_80">246px</dimen><dimen name="height_42_80">252px</dimen> <dimen name="height_43_80">258px</dimen><dimen name="height_44_80">264px</dimen> <dimen name="height_45_80">270px</dimen><dimen name="height_46_80">276px</dimen> <dimen name="height_47_80">282px</dimen><dimen name="height_48_80">288px</dimen> <dimen name="height_49_80">294px</dimen><dimen name="height_50_80">300px</dimen> <dimen name="height_51_80">306px</dimen><dimen name="height_52_80">312px</dimen> <dimen name="height_53_80">318px</dimen><dimen name="height_54_80">324px</dimen> <dimen name="height_55_80">330px</dimen><dimen name="height_56_80">336px</dimen> <dimen name="height_57_80">342px</dimen><dimen name="height_58_80">348px</dimen> <dimen name="height_59_80">354px</dimen><dimen name="height_60_80">360px</dimen> <dimen name="height_61_80">366px</dimen><dimen name="height_62_80">372px</dimen> <dimen name="height_63_80">378px</dimen><dimen name="height_64_80">384px</dimen> <dimen name="height_65_80">390px</dimen><dimen name="height_66_80">396px</dimen> <dimen name="height_67_80">402px</dimen><dimen name="height_68_80">408px</dimen> <dimen name="height_69_80">414px</dimen><dimen name="height_70_80">420px</dimen> <dimen name="height_71_80">426px</dimen><dimen name="height_72_80">432px</dimen> <dimen name="height_73_80">438px</dimen><dimen name="height_74_80">444px</dimen> <dimen name="height_75_80">450px</dimen><dimen name="height_76_80">456px</dimen> <dimen name="height_77_80">462px</dimen><dimen name="height_78_80">468px</dimen> <dimen name="height_79_80">474px</dimen><dimen name="height_80_80">480px</dimen> </resources>
<?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>
待续。。。。。。。。。。。。。。。。。。。。。。