简体   繁体   English

将ImageView对准屏幕右上方

[英]Align ImageView to top right of screen

I've a imageView in my layout which serves as a background which doesn't scale. 我的布局中有一个imageView,它用作不缩放的背景。 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的xml:

    <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. 我还尝试将其放置在具有gravity =“ top | right”的线性布局中,但不会消除图片周围的空白,因为它具有match_parent大小。 I also need to keep the scale of the picture, so I can't do fill_parent. 我还需要保持图片的比例,所以我不能执行fill_parent。

I hope someone can help me with this! 我希望有人可以帮助我!

Maybe add 也许加上

android:adjustViewBounds="true" 

to your ImageView ? 到您的ImageView I had a similar problem. 我有一个类似的问题。

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_alignParentRight and layout_alignParentTop set to true on the view itself. 重要的部分是包装的RelativeLayout以及在视图本身上设置为true的layout_alignParentRightlayout_alignParentTop

This won't wrap your complete view yet since it's all wrap_content . 由于所有内容均为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). 您可以针对每种屏幕类型使用不同的大小资源,也可以使用不同的 layout_widthlayout_height ,然后在需要时使用scaleType属性(可能通过fitXY缩放图像以匹配视图)。

Don't use a LinearLayout if not necessary. 如有必要,请勿使用LinearLayout Documentation on RelativeLayout can be found here . 有关RelativeLayout文档可以在这里找到

具有linearlayout以使imageview与图像配合使用

android:scaleType="fitXY"

I'd like to point out that hcpl's answer is right although I wasn't being able to achieve that because I had set gravity of the RelativeLayout to Center so it was centralizing every child view to center. 我想指出的是,尽管我无法实现hcpl的答案是正确的,因为我已将RelativeLayout的引力设置为Center,所以它会将每个子视图都居中。 So if you who came here having the same issue please, pay attention to the parent view. 因此,如果您来这里时遇到同样的问题,请注意父视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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