How to eliminate extra space formed in a Linearlayout when creating Imageview via code?

Asked

Viewed 126 times

1

I’m using this code to create a 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 that LinearLayout did not accompany the imageView and there was a space above and below the image.

follow the screenshots of the screen

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

xml code

<RelativeLayout xmlns: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 eliminate that space?

  • It is necessary to have these Linearlayout all? Can’t just be one and put there the images all?

  • @ramaral yes because sometimes I play text there

1 answer

2


Initially I thought it was a problem of layout but not.

The image is being resized when it is assigned to Imageview, however its limits(Bounds) keep the original dimensions, making the Layout which contains an adjustment of its dimensions according to them.

It is necessary to indicate the Imageview adjust its limits to the size of the resized image, through 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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.