如何以编程方式在 LinearLayout 中添加 ImageView 而不需要额外的空格?

发布于 2024-12-23 05:36:23 字数 2045 浏览 6 评论 0原文

我正在尝试以编程方式在具有垂直方向的 LinearLayout 内添加 ImageView 。我的布局文件描述为下面的 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:paddingTop="5dip"
   android:id="@+id/mainView"">

   <TextView android:id="@+id/tvTituloInformacao"
    android:layout_width="fill_parent"          
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textStyle="bold"
    android:textColor="@color/white"
    android:gravity="center_horizontal"
    android:shadowColor="@color/black_translucent"
    android:shadowDx="2.0"
    android:shadowDy="2.0"
    android:shadowRadius="3.0"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true" /> 

    <LinearLayout android:id="@+id/resourceContainer"
    android:layout_width="fill_parent"          
    android:layout_height="wrap_content"
    android:layout_marginTop="5dip"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="10dip"
    android:layout_marginBottom="10dip"
    android:padding="3dip"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_below="@+id/tvTituloInformacao"
    android:background="@color/black_translucent2" /> 

 </RelativeLayout>

我的将 ImageView 添加到 LinearLayout 的 java 源代码如下:

for(Resource r : mUser.getPictures()) {
    ImageView img = new ImageView(this);
    img.setVisibility(View.VISIBLE);
    img.setTag(r.getThumb60());
    imageLoader.DisplayImage(r.getThumb60(), this, img);
    ((LinearLayout) tempFotosView.findViewById(R.id.resourceContainer)).addView(img);
}

发生的情况是,在每个 ImageView 之前和之后,我得到了一些不应该存在的额外空格,因此高度图像容器的数量增加了很多。我所做的一项测试得到了预期的结果,即不使用 imageLoader,而是以编程方式将 ImageResource 设置为静态图像文件。这样我就没有获得任何额外的空间。

奇怪的结果如下图所示:

在此处输入图像描述

有没有办法不添加这个额外的空间?

I am trying to add ImageView programatically inside a LinearLayout, which has vertical orientation. My layout file is describe as the xml below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:paddingTop="5dip"
   android:id="@+id/mainView"">

   <TextView android:id="@+id/tvTituloInformacao"
    android:layout_width="fill_parent"          
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textStyle="bold"
    android:textColor="@color/white"
    android:gravity="center_horizontal"
    android:shadowColor="@color/black_translucent"
    android:shadowDx="2.0"
    android:shadowDy="2.0"
    android:shadowRadius="3.0"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true" /> 

    <LinearLayout android:id="@+id/resourceContainer"
    android:layout_width="fill_parent"          
    android:layout_height="wrap_content"
    android:layout_marginTop="5dip"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="10dip"
    android:layout_marginBottom="10dip"
    android:padding="3dip"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_below="@+id/tvTituloInformacao"
    android:background="@color/black_translucent2" /> 

 </RelativeLayout>

And my java source code that adds the ImageViews to the LinearLayout is as follow:

for(Resource r : mUser.getPictures()) {
    ImageView img = new ImageView(this);
    img.setVisibility(View.VISIBLE);
    img.setTag(r.getThumb60());
    imageLoader.DisplayImage(r.getThumb60(), this, img);
    ((LinearLayout) tempFotosView.findViewById(R.id.resourceContainer)).addView(img);
}

What happens is that before and after each ImageView I am getting some extra spaces that should not be there, so the height of the images container increases a lot. One test that I did and got the expected result was not to use the imageLoader and instead I set the ImageResource programatically to a static image file. This way I didn't get any extra space.

The weird result is illustrated in the image below:

enter image description here

Is there a way to not add this extra space?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恰似旧人归 2024-12-30 05:36:24
  ((LinearLayout)findViewById(R.id.resource)).add(img);
  ((LinearLayout)findViewById(R.id.resource)).add(img);
饮湿 2024-12-30 05:36:23

我认为您在图像视图中加载的图像可能在顶部和底部有一些额外的间距,并且其他假设您在 imageview 中加载图像,分辨率与屏幕尺寸不同,并将 ImageView 的scaleType设置为centerInside(如果是)如果是这样,请尝试使用以下方法固定图像视图的高度,并将scaleType设置为fixXY。

I think images you are loading in image views may have some extra spacing on top and bottom, and other assumption you are loading images in imageview, with different resolution than your screen-size, and setting scaleType of ImageView to centerInside, if it is the case please try using following by fixing height of the image view as well, and set scaleType to fixXY.

因为看清所以看轻 2024-12-30 05:36:23

当 ImageView 构建后图像比屏幕尺寸宽时,就会发生这种情况。 Android 会根据纵横比缩小图像的尺寸,但不会删除之前分配的图像周围的空间。

要获得更清晰的修复,请在 imageLoader 之后尝试此代码

This happens when the image is wider than the screen size after the ImageView has been built. Android shrinks the image's dimensions accordingly to aspect ratio, but doesn't remove the allocated space around it earlier.

For a cleaner fix, try this code after your imageLoader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文