我正在研究一個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設備上(這是我希望它看起來像以及):
是什麼原因造成的藍色方塊的大小懸殊?有任何想法嗎?謝謝!
模擬器運行的是什麼版本,運行的Android設備和版本是什麼? – zostay
對不起,該設備是運行Android 2.2.1的LG Optimus V手機。模擬器運行Android 2.2。 – David