android - Android 中的 Listview 在某些行的顶部和底部添加了额外的空间

标签 android android-layout

我的应用程序的一个屏幕显示一个包含 6 列的 ListView ,第六列是一个包含用户创建的图片或草图的 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>

除一种情况外,此设置效果很好:显示的图像是由相机拍摄的垂直方向的照片。在那种情况下, ListView 会在行内图像的上方和下方抛出一堆空白。在 Activity 中,我使用 custumViewBinder 使用从此处获得的代码将图像显示为位图: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;
    }

}

在该 Activity 中,我为宽度和高度传递了 200 和 150 的值。我的问题似乎是,当我几个月前最初编写这段代码时,我只将它设计为适用于横向图片,现在它也需要适用于纵向图片。

最佳答案

根据我的 post 中 stoney80 的建议在 r/androiddev 上,我添加了行

android:adjustViewBounds="true"

到 imageVew,这让它完全符合我的期望。

关于android - Android 中的 Listview 在某些行的顶部和底部添加了额外的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14654593/

相关文章:

android - 使用 Intent.ACTION_SENDTO 时在哪里指定电话号码?

Android Studio 卡在 "Downloading Components"

android - 如何处理回收站 View 中的空数据?

android - 如何在android中将输入类型数字更改为EditText的文本

android - Proguard 对已删除的类发出警告

android - 约束布局 : Realign baseline constraint in case if dependent view visibility was set to GONE

java - 如何在Android版Cocos2d中复制CCSprite?

android - 如何在LinearLayout中获取ImageView的坐标和大小

android - 每个布局资源都需要一个菜单​​资源吗

android - 日期选择器和日历中的日期格式更改