Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align ImageView to top right of screen

I've a imageView in my layout which serves as a background which doesn't scale. But I need to have my image in the topright corner of the screen. I now have my image in the top left corner of the screen. This is my xml for the imageview:

    <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:src="@drawable/achtergrond"
android:scaleType="fitCenter" />

I also tried to put it in a linearlayout which has gravity="top|right" but that doesn't remove the white space around the picture because it's with the match_parent sizes. I also need to keep the scale of the picture, so I can't do fill_parent.

I hope someone can help me with this!

like image 797
Marc Avatar asked May 25 '13 07:05

Marc


2 Answers

Maybe add

android:adjustViewBounds="true" 

to your ImageView? I had a similar problem.

like image 139
osolmaz Avatar answered Oct 27 '22 12:10

osolmaz


The following layout will position your image (or any other view) at the top right corner of the screen. Check comments below code block for more details.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

The important parts are the wrapping RelativeLayout and the layout_alignParentRightand layout_alignParentTopset to true on the view itself.

This won't wrap your complete view yet since it's all wrap_content. You can use different size resources per screen type or work with different layout_width and layout_height and then use the scaleType property as you whish (probably fitXY to scale image to match the view).

Don't use a LinearLayout if not necessary. Documentation on RelativeLayoutcan be found here.

like image 32
hcpl Avatar answered Oct 27 '22 10:10

hcpl