2011-12-25 95 views
2

我正在研究一個Android應用程序,其中我看到在實際設備上顯示的內容與在模擬器上運行應用程序時顯示的內容之間存在一些差異。Android:模擬器UI和設備UI之間的差距

這裏是相關的代碼。

我有限定的形狀的xml文件 - 一個簡單的藍色矩形:

<?xml version="1.0" encoding="utf-8"?> 

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <corners 
     android:radius="4dp" /> 
    <gradient 
     android:angle="270" 
     android:startColor="#449def" 
     android:endColor="#2f6699" /> 
    <stroke 
     android:width="1dp" 
     android:color="#2f6699" /> 
</shape> 

我然後有一個使用另一種「視圖」的XML文件,該文件的形狀,這裏示出:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <View 
     android:background="@drawable/blue_rectangle" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
    /> 

</LinearLayout> 

最後,我有一個擴展了RelativeLayout的自定義ViewGroup。這個自定義佈局具有上面描述的視圖的子視圖對象。自定義佈局也會覆蓋onLayout方法,以便它自己處理佈局過程。該代碼是在這裏:

@Override 
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) 
{ 
    //Layout the children of this viewgroup 
    int childCount = this.getChildCount(); 
    for (int i = 0; i < childCount; i++) 
    { 
     NodeView view = (NodeView) this.getChildAt(i); 
     view.layout(40, 40, 40, 40); //Hard code the child object position 
    } 
} 

正如你所看到的,我硬編碼子視圖具有位置(40,40),並具有寬度和40傾角的高度。

不幸的是,我得到了不同的結果。這是它看起來像Android模擬器(的Android開發工具的一部分):

Android emulator output

這是它看起來像我的實際Android設備上(這是我希望它看起來像以及):

Android device output

是什麼原因造成的藍色方塊的大小懸殊?有任何想法嗎?謝謝!

+0

模擬器運行的是什麼版本,運行的Android設備和版本是什麼? – zostay

+0

對不起,該設備是運行Android 2.2.1的LG Optimus V手機。模擬器運行Android 2.2。 – David

回答

3

當您製作名爲Abstracted LCD Density的AVD時,仿真器有一個設置。確保設置與設備相同。看看是否有效。

+1

謝謝!我創建了一個更精確地反映我的手機的AVD(320x480分辨率,180dpi),並且效果很好。但他們如何期望我們開發多種顯示分辨率?藍色盒子的大小和每個帶有橙色邊框的白色矩形的大小都應該是40x40 dip ...但是與其他AVD的大小不同。這怎麼解釋? – David

+0

此鏈接將幫助解釋它http://developer.android.com/guide/practices/screens_support.html –

相關問題