我正在尝试以编程方式在具有垂直方向的 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>
我将 ImageViews 添加到 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 设置为静态图像文件。这样我就没有得到任何额外的空间。
奇怪的结果如下图所示:
有没有办法不添加这个额外的空间?
最佳答案
我认为您在 ImageView 中加载的图像可能在顶部和底部有一些额外的间距,并且假设您在 ImageView 中加载图像,分辨率与屏幕尺寸不同,并将 ImageView 的 scaleType 设置为 centerInside,如果在这种情况下,请尝试通过固定 ImageView 的高度来使用以下方法,并将 scaleType 设置为 fixXY。
关于android - 如何在没有额外空格的情况下以编程方式在 LinearLayout 中添加 ImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8667645/