Why is 1px sometimes 2px when specified in Android XML?

Posted by Daniel Lew on Stack Overflow See other posts from Stack Overflow or by Daniel Lew
Published on 2010-04-23T14:48:07Z Indexed on 2010/04/23 15:53 UTC
Read the original article Hit count: 487

Filed under:

I've got a desire for a one-pixel divider line, just for looks. I thought I could accomplish this using a View of height 1px, with a defined background. However, I'm getting some very odd behavior on different devices - sometimes the 1px ends up as 2px.

Take this sample layout for example:

<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>

When run on my G1, this comes out fine. But on the Nexus One, it alternates between 1px lines and 2px lines.

Does anyone know where this is going awry? Why does Android sometimes make 1px into 2px?

© Stack Overflow or respective owner

Related posts about android