我試圖在垂直方向的LinearLayout內編程添加ImageView。我的佈局文件是描述爲下面的XML:如何以編程方式在LinearLayout中添加ImageView而無需額外空格?
<?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>
我的Java源代碼,添加ImageViews到的LinearLayout情況如下:
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以編程方式設置爲靜態圖像文件。這樣我沒有得到任何額外的空間。
怪異的結果如下圖所示:
有沒有辦法不添加此額外的空間?
您應該將LinearLayout緩存在For循環之外以提高性能。 – Subby 2015-01-06 21:14:45