Android在RelativeLayout中将图像对齐到顶部和中心

8

我想将图片与我的相对布局顶部对齐,然后在水平方向上居中。以下是我使用的代码:

<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"  />

但是这段代码并没有生效。我的图片被居中了,但是并没有对齐到相对布局的顶部。我尝试使用 android:scaleType="fitStart",但实际上它只是将图像左对齐和上对齐到其父级。

那么,有什么想法可以使图片正确对齐吗?

另外,我忘了提到我是这样设置图片到我的 ImageView 中的:

    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);
1个回答

19

除了您分配的内容区域外,代码似乎完美无缺。 fill_parent 会占据整个视图并居中显示,因此只需将其修改为 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"  />

没问题,但我希望图片至少能够适应其父元素的宽度。 - Android-Droid
你可以通过 android:layout_width="fill_parent" 和 android:layout_height="wrap_content" 来实现。 - Mohammed Azharuddin Shaikh
实际上,我现在将宽度和高度都设置为fill_parent,它起作用了...我不知道为什么之前在代码中没有更改任何其他内容时它不起作用。无论如何,还是谢谢你的回答! - Android-Droid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接