Android:模拟器UI和设备UI之间的差异

时间:2011-12-25 04:46:31

标签: android-layout android-emulator android-ui android

我正在开发一个Android应用程序,在这个应用程序中,我看到实际设备上显示的内容与我在模拟器上运行应用程序时显示的内容之间存在差异。

以下是相关代码。

我有一个定义形状的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>

然后我有另一个使用该形状的“view”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>

最后,我有一个自定义ViewGroup,它扩展了RelativeLayout。此自定义布局具有我上面描述的视图的子视图对象。自定义布局还会覆盖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

导致蓝盒大小不一致的原因是什么?有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:3)

当您制作AVD称为“抽象LCD密度”时,模拟器会进行设置。确保将其设置为与设备相同。看看是否有效。