1

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

zorglub76
  • 4,852
  • 7
  • 37
  • 46

3 Answers3

4

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"
Community
  • 1
  • 1
Romain Piel
  • 11,017
  • 15
  • 71
  • 106
2

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".

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'm still getting 2px line between the pic and the top of the RelativeLayout. You can see what I'm talking about here: http://img706.imageshack.us/img706/2852/20100112144811.png – zorglub76 Jan 12 '10 at 13:52
  • Without the images and layouts, I cannot really help much. Ensure that there is no transparent segment at the top of the image, that you're not applying padding or margins on the ImageView, etc. – CommonsWare Jan 12 '10 at 14:11
  • OK, the layout I'm working on is a list item. You can see XML file here: http://pastebin.com/m239fa5c and the screenshot of the resulting layout here: http://img706.imageshack.us/img706/2852/20100112144811.png – zorglub76 Jan 12 '10 at 15:25
  • Make sure that when you inflate your row, you use inflate(R.layout.whatever, parent, false). – CommonsWare Jan 12 '10 at 15:33
  • I have `convertView = inflater.inflate(R.layout.result_list_row, ResultListActivity.this.getListView(), false);` but the result is still the same... – zorglub76 Jan 12 '10 at 16:22
  • Your second parameter should be the `parent` parameter passed into getView() (or newView(), depending on which adapter you're using). I *think* that will be the same as what you have, but I recommend making the change to be sure. Get this wrong, and RelativeLayout gets cranky. Beyond that, I have no suggestions. – CommonsWare Jan 12 '10 at 16:33
  • ..and again the same (when using "parent" instead of "getListView()").. Ok, I'll try to reproduce this behavior in a clean new project and then file it as a bug. If I manage to reproduce it, that is.. – zorglub76 Jan 13 '10 at 08:23
  • I reported it here: http://code.google.com/p/android/issues/detail?id=6020 There's also a project attached to the reported issue (if you'd like to play with it) – zorglub76 Jan 13 '10 at 10:26
2

Damn!!

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

zorglub76
  • 4,852
  • 7
  • 37
  • 46
  • Actually - it doesn't work. fitStart aligns image to the upper left corner. But I gave up. Anyway, it looks better center-aligned... – zorglub76 Jan 27 '10 at 15:48