将ImageView对齐屏幕右上角

Mar*_*arc 7 android imageview android-layout

我的布局中有一个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" />
Run Code Online (Sandbox Code Playgroud)

我也尝试将它放在一个linearlayout中,它具有gravity ="top | right"但不会删除图片周围的空白区域,因为它具有match_parent大小.我还需要保持图片的比例,所以我不能做fill_parent.

我希望有人可以帮助我!

oso*_*maz 23

也许添加

android:adjustViewBounds="true" 
Run Code Online (Sandbox Code Playgroud)

到你的ImageView?我遇到了类似的问题.


hcp*_*cpl 11

以下布局将您的图像(或任何其他视图)定位在屏幕的右上角.查看代码块下方的注释以获取更多详细信

<?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>
Run Code Online (Sandbox Code Playgroud)

重要的部分是包装RelativeLayout,layout_alignParentRight并且layout_alignParentTop在视图本身上设置为true.

这不会包装你的完整视图,因为它是全部wrap_content.您可以使用每个屏幕类型不同尺寸的资源或工作有不同的layout_widthlayout_height,然后使用scaleType你哗哗财产(可能fitXY缩放图像匹配视图).

LinearLayout如果没有必要,请不要使用.文档RelativeLayout可以在这里找到.


sti*_*ike 0

使用 Linearlayout 使图像视图与图像使用相匹配

android:scaleType="fitXY"
Run Code Online (Sandbox Code Playgroud)