2012-05-31 152 views
1

我有这个列表视图,其中图像视图不适合屏幕宽度,即使位图的大小大于屏幕大小。下面是结果我得到:Android列表视图图像不适合屏幕宽度

(由于垃圾邮件的政策,我不能在这里后我的截图,请点击下面的链接查看截图。) http://www.sundancepost.com/ivue/screen/screen1.jpg

如图中红色框指示屏幕上方,该列表不适合屏幕的宽度。

以下是我想要的结果:

http://www.sundancepost.com/ivue/screen/screen2.jpg

这里是我的布局代码。

featured_listrow.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical">  

<ImageView 
    android:id="@+id/banner"   
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content" 
    android:adjustViewBounds="true" 
    android:src="@drawable/ampersand_banner"/> 

</RelativeLayout> 

featured_list:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<ListView 
    android:id="@+id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:smoothScrollbar="true" 
    > 

</ListView> 

</LinearLayout> 

我相信我已经正确设置layout_width和layout_height两者的布局文件。我认为这与运行时设置的android:adjustViewBounds有关。有人能帮帮我吗?

+0

'android:scaleType =“fitXY”' –

+0

不幸的是,结果仍然是一样的。即使当我调整图像大小以适合屏幕宽度时,也会发生这种情况。 –

+0

尝试玩scaleType的值。另外,请显示listVIew的适配器的代码。我不认为xml有什么问题。 –

回答

2

我终于明白了问题所在。图像的缩放发生在这ImageLoader.classhttp://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

所以,当我注释掉了代码的缩放部分,这一切都恢复正常。

这是代码:

private Bitmap decodeFile(File f){ 
    try { 
     //decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

     //Find the correct scale value. It should be the power of 2. 
     // final int REQUIRED_SIZE=100; 
     // int width_tmp=o.outWidth, height_tmp=o.outHeight; 
     int scale=1; 
     //while(true){ 
     //if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) 
     //break; 
     //width_tmp/=2; 
     //height_tmp/=2; 
     //scale*=2; 
     //} 

     //decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) {} 
    return null; 
} 
0

我认为唯一可以帮助的是使用其他程序调整图像大小。问题在于图像不能被裁剪,只能在xml或代码中设置图像大小时进行水平或垂直两侧的编程。

+0

我没有通过photoshop调整图像大小。我的图片的当前宽度是480px,这应该很适合我的android设备。但是,正如它发生的那样,即使使用正确的图像尺寸,图像仍然不适合屏幕。 –

+0

有趣的是,当我将featured_listrow.xml切换到图形布局时,图像看起来非常适合屏幕的宽度。但是,它不会在运行时发生。 –

1

您可以设置图像在图像中任一视图中使用

1)android:scaleType="fitXY"

2)android:scaleType="centerCrop"

在你的图像视图中使用任何一个。它让我完美的结果,因为我想要的。