安卓中的ListView在某些行顶部和底部添加了额外的空白间距

3

我的一个应用程序界面显示了一个包含6列的列表视图,第六列是一个imageView,其中包含用户创建的图片或草图。以下是listView行的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="2dp"
    android:paddingBottom="2dp" >

    <TextView android:id="@+id/surveyColumn1"
        android:textSize="13sp"
        android:layout_width="0dip"
        android:layout_weight="0.105"
        android:layout_height="wrap_content"/>

    <TextView android:id="@+id/surveyColumn2"
        android:textSize="13sp"
        android:layout_width="0dip"
        android:layout_weight="0.137"
        android:layout_height="wrap_content"/>

    <TextView android:id="@+id/surveyColumn3"
        android:textSize="12sp"
        android:layout_width="0dip"
        android:layout_weight="0.25"
        android:layout_height="wrap_content"/>

    <TextView android:id="@+id/surveyColumn4"
       android:textSize="13sp"
        android:layout_width="0dip"
        android:layout_weight="0.153"
        android:layout_height="wrap_content"/>

    <TextView android:id="@+id/surveyColumn5"
        android:textSize="13sp"
        android:layout_width="0dip"
        android:layout_weight="0.153"
        android:layout_height="wrap_content"/>

    <ImageView android:id="@+id/surveyColumn6"
        android:layout_width="0dip"
        android:layout_weight="0.202"
        android:layout_height="wrap_content"
        android:paddingTop="0dp"
        android:paddingBottom="0dp"
        android:scaleType="centerInside"
        android:layout_gravity="center"
        android:contentDescription="@string/pictureHeader"/>

</LinearLayout>

这个设置很好用,但有一个问题:当显示的图片是竖直方向照相机拍摄的照片时,列表视图会在行内图片上下添加一些空白。在这个活动中,我使用自定义视图绑定器来将图片作为位图显示,使用的代码从这里获得:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
目前,我唯一找到的消除空间的方法是将imageView的高度设置为静态值,但我不想这样做,因为有些行没有图片。能找到问题所在的人就是我的英雄。
编辑:这是我调用以将图片显示为位图的类:
public class customBitmap {

    public customBitmap(String pathName, int width, int height, ImageView view) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(pathName, options);

        options.inSampleSize = calculateInSampleSize(options, width, height);

        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile(pathName, options);

        //Check if bitmap was created; if so, display it in the imageView
        if(bitmap == null)
            Log.w("UI Thread", "Null bitmap at moto.sitesurvey.customBitmap:35");
        else
            view.setImageBitmap(bitmap);
    }

    public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight){
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if(height > reqHeight || width > reqWidth) {
            if(width > height)
                inSampleSize = Math.round((float)height / (float)reqHeight);
            else
                inSampleSize = Math.round((float)width / (float)reqWidth);
        }

        return inSampleSize;
    }

}

在这个活动中,我传递了200和150作为宽度和高度的值。我的问题似乎是,当我几个月前编写这段代码时,我只设计它用于横向照片,现在它也需要支持纵向照片。
2个回答

2

根据stoney80在我在r/androiddev上的帖子中的建议,我添加了这行代码。

android:adjustViewBounds="true"

针对imageView进行了操作,这样就达到了我所期望的效果。


0
你在将图片放入视图之前调整了它们的大小吗?如果是这样,请检查并确定图片的最佳尺寸,并尝试将从垂直摄像头拍摄的位图调整为该尺寸。
如果这不能解决问题,我可以查看你的ImageView和Bitmap代码吗?

我正在调整它们的大小(请参见新附加的代码),我猜200和150对于宽度和高度来说效果很好,至少对于横向图片是这样。如何测试垂直拍摄的相机镜头? - VTDoubleE

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接