开发者

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

开发者 https://www.devze.com 2022-12-28 11:40 出处:网络
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

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_hei开发者_如何学编程ght="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?

Edit: To make this absolutely clear, I do not care about solving my example in particular; what I care about is being able to say "this View shall be 1px high", in any circumstance, not just this example.


Are you building against SDK 1.5, or setting android:anyDensity="false"? If your app does not say it supports densities, then Android will run in compatibility mode where it makes the screen look like an mdpi screen, scaling ALL coordinates and drawing to match the real screen density. Running in an hdpi screen like Droid, this is a 1.5x scaling, so every other "pixel" will be mapped to two pixels on the screen.

Apps today should be written to support different screens, in which case the px and dp units will behave as desired on all screens.


I would imagine that this is occurring where the line is on the edge between pixels ; is this occuring because you are mixing dp (density-independant) pixel measurements with absolutes? If the devices have different dp / pixel ratios your margins (defined in dps) could be placing your separators on the edge between pixels.

Try changing your margins to 4px instead of 4dp.


I know this is not the question, but if you are using a ListView, you have some properties for dividers:

android:divider="" /*color, drawable, etc*/
android:dividerHeight="1dip"

Plus you really shouldn't use Pixels but independant density measurement..

0

精彩评论

暂无评论...
验证码 换一张
取 消