Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images height too tall

Tags:

android

xml

I am trying to setup a section of a screen with 4 images around each other but there is huge white space between them as per image example:

example of problem

I would like them all right next to each other. Below is the XML I am using:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
          android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/imageView1"
        android:src="@drawable/test1"
        android:layout_weight="1"/>
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/imageView2"
        android:src="@drawable/test"
        android:layout_weight="1"/>

</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/imageView3"
        android:src="@drawable/test1"
        android:layout_weight="1"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/imageView4"
        android:src="@drawable/test"
        android:layout_weight="1"/>
</LinearLayout>

I am also trying to get them to the top of the screen but that white space is preventing that.

The images you see dont have any white space at all, the edge of the colors are the edge of the image?

Thanks

like image 437
Stillie Avatar asked Dec 06 '22 20:12

Stillie


2 Answers

You need to set the scaleType to centerInside and the adjustViewBounds to true like this: (adjust view bounds should get rid of the space you don't want.)

   <ImageView
        ...
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:adjustViewBounds="true" />
like image 113
Jens Avatar answered Jan 12 '23 00:01

Jens


@x10sion:For your information,

Why should i add this

android:scaleType="centerInside" is going to center the image inside the container, rather than making the edges match exactly. Reference

android:adjustViewBounds="true" this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

like image 36
IntelliJ Amiya Avatar answered Jan 11 '23 23:01

IntelliJ Amiya