dip(dp): device independent pixels(設備獨立像素)php
dip,就是把屏幕的高分紅480分,寬分紅320分。好比你作一條160dip的橫線,不管你在320還480的模擬器上,都是一半屏的長度。html
dpi:dot per inchandroid
dpi=(√(橫向分辨率^2+縱向分辨率^2))/屏幕尺寸)佈局
ppi:pixels per inch(跟dpi同樣)字體
計算了一下小米手機屏幕的PPI,4.0英寸、分辨率854X480,PPI(DPI)spa
=√(854^2+480^2)/4=244.912……≈245.
code
px:pixelxml
sp:scaled pixels(放大像素),主要用於字體顯示。htm
pt:point,是一個標準的長度單位,1pt=1/72英寸,用於印刷業。blog
日常所說的hdpi等劃分方法(按DPI來劃分):
關於分辨率適配:
<?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>
<?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>
整個界面佈局看起來很是直觀,只是嵌套的邏輯要本身理下。顯示效果以下圖,其中左面一個是480x800的界面,右面的是320x480的界面(後面的圖也如此),能夠看出顯示比例和代碼中徹底一致,我就很少說了,你們對照下就能看出來了。
2、Java代碼裏動態佈局
// 第一個按鈕,寬度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);
參考文章:
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=173973