5

我試圖在垂直方向的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以編程方式設置爲靜態圖像文件。這樣我沒有得到任何額外的空間。

怪異的結果如下圖所示:

enter image description here

有沒有辦法不添加此額外的空間?

+0

您應該將LinearLayout緩存在For循環之外以提高性能。 – Subby 2015-01-06 21:14:45

回答

3

我認爲你是加載在圖像視圖圖像可能對頂部和底部,以及其他假設您加載圖像在imageview的一些額外的空間,以不同的分辨率屏幕大小和ImageView的的設置scaleType如果是這種情況,請嘗試使用以下方式固定圖像視圖的高度,並將scaleType設置爲fixXY。

-2
((LinearLayout)findViewById(R.id.resource)).add(img); 
+4

沒有像'add'這樣的方法;發佈一些答案之前,你應該知道你說什麼。 – 2013-02-21 19:25:33

0

當ImageView構建完成後,圖像寬於屏幕尺寸時會發生這種情況。 Android會將圖像的尺寸縮小爲縱橫比,但不會先移除其周圍的分配空間。

要獲得更清晰的修補程序,請在imageLoader後嘗試this code

相關問題