Android aligns image at top and center of RelativeLayout

I am trying to align the image at the top of my Relative Layout and after that center horizontally. Here is the code I use for:

<ImageView android:id="@+id/collection_image_background" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> 

but this code does not work. My image is centered but not aligned at the top of my relative layout. I tried using android:scaleType="fitStart" , but actually aligned the image to the left and top of its parent.

So, any idea how I can align the image correctly when I need to?

PS I forget to mention that I set the image to my ImageView as follows:

  BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inTempStorage = new byte[8*1024]; ops = BitmapFactory.decodeStream(cis,null,o2); ImageView view = (ImageView) findViewById(R.id.collection_image_background); view.setImageBitmap(ops); 
+7
source share
1 answer
The code

seems perfect except for the content area that you assigned fill_parent takes the whole look and will appear as center, so just change it to wrap_content

  <ImageView android:id="@+id/collection_image_background" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> 
+18
source

All Articles