zorglub76
zorglub76

Reputation: 4942

How to align image?

I've got thumbnails which larger side is 70px (e.g 70x40 or 52x70).

I need to create a gray box 70x70px and put the thumbnail in it, so that image is aligned with the box at top vertically and center horizontally.

How to do that?

I've tried with an ImageView 70x70px, gray background, but image is not positioned as it should (it's in vertical and horizontal center, instead of vertical top and horizontal center).

I've also tried wrapping it with LinearLayout 70x70px, gray background and then positioning it, but then i get 1 or 2px line between LinearLayout and ImageView. I tried to set padding and margins to 0, but the gray line stays on...

Upvotes: 1

Views: 9813

Answers (3)

Romain Piel
Romain Piel

Reputation: 11177

I had exactly the same issue and found the solution in that thread. For those who struggle with that problem, just add this line to your ImageView:

android:adjustViewBounds="true"

Upvotes: 4

zorglub76
zorglub76

Reputation: 4942

Damn!!

Of course, it was so simple: just had to add android:scaleType="fitStart" to ImageView - no need for all those layouts..

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1006674

Use a RelativeLayout. Set the background to be gray and the size to be 70px square. Put the ImageView as a child of the RelativeLayout, with android:layout_alignParentTop="true" and android:layout_centerHorizontal="true".

Upvotes: 2

Related Questions