2012-04-18 98 views
7

我正在嘗試將圖像對齊到相對佈局的頂部,然後在水平方向居中。這裏是我使用的代碼:Android在頂部和中心在RelativeLayout中對齊圖像

<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",但它實際上是將圖像的左側和頂部對齊。

因此,任何想法如何我可以正確對齊圖像,因爲我需要?

P.S.我忘了提,我的圖片設置爲我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); 

回答

18

代碼似乎是,除非你有指定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" /> 
+0

沒關係,但我希望圖像適合寬度至少是父母。 – 2012-04-18 12:47:01

+0

你可以通過android:layout_width =「fill_parent」和android:layout_height =「wrap_content」來完成。 – 2012-04-18 12:48:35

+0

實際上我現在設置寬度和高度'fill_parent',它的工作......不知道爲什麼它不工作之前,當我沒有觸及代碼中的其他任何東西。無論如何感謝您的答案! – 2012-04-18 12:50:59