android ImageView:将图像放在顶部

android ImageView:将图像放在顶部,android,Android,我有一个活动只包含一个ImageView。此视图应显示从URL加载的图像。我想,这个图像将显示在我的屏幕顶部使用整个屏幕宽度。 我的布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"

我有一个活动只包含一个
ImageView
。此视图应显示从URL加载的图像。我想,这个图像将显示在我的屏幕顶部使用整个屏幕宽度。 我的布局文件:

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/overview_bgColor" >
        <ImageView 
            android:id="@+id/overview_image"
            android:contentDescription="image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top|fill_horizontal"/>
    </LinearLayout>

但图像总是垂直居中显示。但是为什么呢?我想,
top
会把这个
ImageView
放在我的屏幕上吗?

试试这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/overview_bgColor" >
    <ImageView 
        android:id="@+id/overview_image"
        android:contentDescription="image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"            
     />
</LinearLayout>


这很有效,非常感谢。但是为什么
top
不将图像视图放在屏幕的顶部呢?它实际上将图像视图放在了顶部,但是视图边界没有设置为保留正确的比率。不过,您可以进行验证,保留旧代码,并将背景添加到imageview中,看看会发生什么,您将看到一个顶部和底部带有背景颜色的大图像。因此,确切的方法是将
android:adjustViewBounds=“true”
添加到我的代码中,或者执行
android:adjustViewBounds=“true”
implicit将
ImageView
放在顶部?adjustViewBounds将直接计算您设置为与父对象匹配的图像的宽度,然后计算其高度以保持比例,否则它会把事情搞砸,而且无论如何,您必须小心安卓中的ImageView,它不会放在顶部