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",但它实际上是左对齐图像和它的父元素的顶部。

那么知道如何根据需要正确对齐图像吗?

PS我忘了提到我正在将图像设置为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);
4

1 回答 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"  />
于 2012-04-18T12:43:20.143 回答