简体   繁体   中英

Android align image in top and center in RelativeLayout

I'm trying to align image to top of my Relative Layout and after that center it horizontally. Here is the code I'm using 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 is not working. My image is centered,but not aligned at top of my Relative Layout. I tried with android:scaleType="fitStart" , but it's actually align imag left and top of it's parent.

So any idea how I can align the image correctly as I need?

PS I forget to mention that I'm setting the image to my ImageView like this :

    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);

code seems to be perfect except content area you had assign fill_parent takes whole view and will appear as center, so just modify it by 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"  />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM