在正常屏幕和大屏幕中混淆主屏幕小部件大小

时间:2010-03-31 22:29:06

标签: android

我正在设计主屏幕小部件。小部件布局文件如下所示。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="240dip" android:layout_height="200dip"
    android:background="@drawable/base_all"
/>

我在HTC Hero设备上运行此小部件,该设备的屏幕为320像素* 480像素,带有mdpi。它在HTC Hero上表现完美。小部件占用3个单元格* 2个单元格空间,即240个像素×200个像素。

然后我在Nexus One设备上运行此小部件,该设备的屏幕为480像素×800像素,mdpi。由于Nexus One也是mdpi,所以我认为240dip相当于Nexus One上的240像素而200dip相当于Nexus One上的200像素,因此小部件不会在Nexus One设备上占用3个单元* 2单元空间。令我惊讶的是,当在Nexus One设备上运行时,小部件在Nexus One设备上采用精确的3个单元格* 2个单元格,大约360像素* 300像素。

我很困惑。上面的布局xml为小部件指定宽度为240dip,高度为200dip,但为什么在Nexus One Device上需要360像素* 300像素?我错过了什么?

感谢。


回复mbaird的评论:

我仍然感到困惑。假设我在窗口小部件中指定了ImageView,picture1,如下所示90dip * 100dip。 Nexus One设备上的ImageView的设备像素尺寸是多少?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="240dip" android:layout_height="200dip"
    android:background="@drawable/base_all"
>
<ImageView android:id="@+id/picture1"
    android:layout_width="90dip"
    android:layout_height="110dip"
    />
</RelativeLayout>

我必须知道这一点,因为在我的程序中我必须在ImageView上绘图:

public void draw(Canvas canvas) {
    ShapeDrawable drawable = new ShapeDrawable(new RectShape());
    drawable.getPaint().setColor(Color.WHITE);
    drawable.setBounds(m_leftTopX, m_leftTopY, m_leftTopX + m_width,
            m_leftTopY + m_height);

    drawable.draw(canvas);
}

据我所知,drawable.setBounds()在其参数中使用了物理像素。我需要知道物理像素尺寸的ImageView大小。

感谢。

1 个答案:

答案 0 :(得分:0)

您正在混合设备独立像素和硬件设备像素。我认为这样做不会太好。

对于英雄占用3x2的小部件将在Nexus One上占用3x2。这就是我在回答您之前的问题时遇到的问题:Home screen widget size for large screen or hdpi?

相关问题