Android Imageview位置未按预期运行

Android Imageview位置未按预期运行,android,Android,我正在使用全屏和锁定的肖像。在舱单中: android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:screenOrientation="portrait" 在布局中,我有一个imageview和两个按钮: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pare

我正在使用全屏和锁定的肖像。在舱单中:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
android:screenOrientation="portrait"
在布局中,我有一个imageview和两个按钮:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"/>
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />
<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />
</LinearLayout>
在我的两个模拟器上,这会按预期在屏幕顶部显示图像

然后绘制位图并替换图像:

sBitmap = Bitmap.createBitmap(557, 580,Config.ARGB_8888);
//draw the bimap image
BitmapDrawable nimage;
nimage = new BitmapDrawable(sBitmap);
imageView.setImageDrawable(nimage); 
在大型仿真器(WVGA800 400x800)上,图像如预期的那样位于顶部

在小型模拟器(QVGA 240x320)和我的手机(240x320)上,图像从顶部向下35像素

为什么它们不同

我可以向上滚动图像:

imageView.scrollTo(0, 35); //this will put image at top
如何找到该值?这样做是行不通的:

int offset = imageView.getTop();// it just = height of image

任何帮助都将不胜感激。

这是因为您的图像对于屏幕来说太大了

在XML中设置android:adjustViewBounds=“true”可能会有所帮助

此外,您可以尝试获取屏幕宽度并使用它:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
Bitmap.createBitmap(width, width/557*580,Config.ARGB_8888);
创建正确大小的图像。同时尝试这两种设置

另一方面,您可以:

imageView = (ImageView) findViewById(R.id.imageView1); //from .xml 
imageView.setImageResource(R.drawable.phone_on);
将代码简化一点

imageView = (ImageView) findViewById(R.id.imageView1); //from .xml 
imageView.setImageResource(R.drawable.phone_on);