How to eliminate extra space formed in a LinearLayout when creating ImageView via code?

1

I'm using this code to create an ImageView and add it to a LinearLayout :

public void inserindoImage(ImageView image,int rid,LinearLayout linear )
{
    LinearLayout.LayoutParams params =
        new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                                      ViewGroup.LayoutParams.WRAP_CONTENT);

    image.setLayoutParams(params);
    image.setImageResource(rid);
    linear.addView(image);
}

Only LinearLayout did not accompany imageView and left a space at the top and bottom of the image.

follows the screen image

xmlcode

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.teste.teste1.Principal"
    android:background="#FFEC0D0D">

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:id="@+id/scrollView">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
                <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linearImage"
                android:orientation="horizontal">
                <!--Imagem vem aqui-->
              </LinearLayout>
               <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linearImage2"
                android:orientation="horizontal">
                <!--Imagem vem aqui-->
              </LinearLayout>
        </LinearLayout>
           </ScrollView>

</RelativeLayout>

How can I delete this space?

    
asked by anonymous 24.03.2016 / 16:59

1 answer

2

At first I thought it was a layout problem but not.

The image is being resized when it is assigned to ImageView , however its bounds ( bounds ) retain the original dimensions, that contains it adjusts its dimensions accordingly.

You must instruct ImageView to adjust its limits to the size of the resized image by setAdjustViewBounds(true) .

Change the code to:

public void inserindoImage(ImageView image, int rid, LinearLayout linear )
{
    LinearLayout.LayoutParams params =
            new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                                          ViewGroup.LayoutParams.WRAP_CONTENT);

    //Caso queira definir uma margem entre imagens
    params.setMargins(0,10,0,0);
    image.setLayoutParams(params);

    image.setImageResource(rid);
    image.setAdjustViewBounds(true);
    linear.addView(image);
}
    
24.03.2016 / 18:31