5

ImageViewをプログラム的にLinearLayout内に追加しようとしていますが、これは垂直方向です。余分なスペースを使わずに、プログラムでImageViewをLinearLayoutに追加する方法は?

<?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> 

とのLinearLayoutにImageViewsを追加し、私のJavaソースコードは以下の通りである:私のレイアウトファイルは、以下のXMLとして記述している

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のIの後でそこにあるべきではない余分なスペースが増えているので、イメージコンテナの高さが大きくなります。私がして、期待された結果を得た1つのテストは、imageLoaderを使用するのではなく、ImageResourceを静的なイメージファイルにプログラム的に設定することでした。私は余分なスペースを得られなかったこのように。この余分なスペースを追加しないようにする方法はあります

enter image description here

奇妙な結果が下の画像に示されていますか?

+0

パフォーマンスを向上させるには、Forループの外部にLinearLayoutをキャッシュする必要があります。 – Subby

答えて

3

私はあなたがイメージビューにロードされている画像は、いくつかの上部と下部に余分なスペース、およびその他の仮定を有することができる、あなたの画面サイズとは異なる解像度で、ImageViewの中に画像をロードし、ImageViewののscaleTypeを設定していると思いますcenterInsideになっている場合は、画像ビューの高さを固定して次のように使用し、scaleTypeをfixXYに設定してみてください。

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

'add'のような方法はありません。いくつかの答えを掲示する前に、あなたが言うことを知っておくべきです。 –

0

これは、ImageViewをビルドした後に画像が画面サイズよりも広い場合に発生します。 Androidは画像のサイズをアスペクト比に応じて縮小しますが、それ以前に割り当てられたスペースは削除しません。

クリーンな修正を行うには、imageLoaderの後にthis codeをお試しください。

関連する問題