为什么在Android XML中指定1px有时是2px?

Dan*_*Lew 3 android

我想要一个像素的分割线,只是为了看起来.我想我可以使用高度为1px的视图来实现这个目标.但是,我在不同的设备上得到了一些非常奇怪的行为 - 有时1px最终为2px.

以此示例布局为例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <View android:layout_width="fill_parent" android:layout_height="1px"
        android:background="@android:color/white" android:layout_marginBottom="4dp" />
    <View android:layout_width="fill_parent" android:layout_height="1px"
        android:background="@android:color/white" android:layout_marginBottom="4dp" />
    <View android:layout_width="fill_parent" android:layout_height="1px"
        android:background="@android:color/white" android:layout_marginBottom="4dp" />
    <View android:layout_width="fill_parent" android:layout_height="1px"
        android:background="@android:color/white" android:layout_marginBottom="4dp" />
    <View android:layout_width="fill_parent" android:layout_height="1px"
        android:background="@android:color/white" android:layout_marginBottom="4dp" />
    <View android:layout_width="fill_parent" android:layout_height="1px"
        android:background="@android:color/white" android:layout_marginBottom="4dp" />
    <View android:layout_width="fill_parent" android:layout_height="1px"
        android:background="@android:color/white" android:layout_marginBottom="4dp" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在我的G1上运行时,这很好.但在Nexus One上,它在1px线和2px线之间交替.

有谁知道这出错了?为什么Android有时会将1px变为2px?

编辑:为了使这个绝对清楚,我不关心特别是解决我的例子; 在任何情况下,我关心的是能够说"这个视图应该是1px高",而不仅仅是这个例子.

hac*_*bod 8

你是针对SDK 1.5构建的,还是设置android:anyDensity ="false"?如果你的应用程序没有说它支持密度,那么Android将在兼容模式下运行,它使屏幕看起来像mdpi屏幕,缩放所有坐标和绘图以匹配真实的屏幕密度.在像Droid这样的hdpi屏幕上运行,这是一个1.5倍的缩放,所以每隔一个"像素"将被映射到屏幕上的两个像素.

应该编写今天的应用程序以支持不同的屏幕,在这种情况下,px和dp单元将在所有屏幕上按照需要运行.