2013-07-23 56 views
4

我正在Android上創建一個活動。我想在其中顯示背景圖像。我把一個ImageView放到我的主要RelativeLayout中。Android ImageView縮放類型centerCrop

<RelativeLayout 
    android:id="@+id/relativeLayoutMain" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/bgImage" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentBottom="true" 
     android:src="@drawable/bg4" 
     android:scaleType="centerCrop" /> 
</RelativeLayout> 

它使得這樣的看法。

enter image description here

我需要查看這樣 enter image description here

我該怎麼辦呢?

+0

將高度和寬度更改爲包裝內容 –

回答

7

嘗試設置ImageView屬性ScaleType=fitEnd

+0

不是評論這樣的答案 –

0

設置android:background="@drawable/bg4"RelativeLayout這樣的:

整個XML應該看起來像:

<RelativeLayout 
    android:id="@+id/relativeLayoutMain" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:background="@drawable/bg4 
    android:orientation="vertical" > 
</RelativeLayout> 
+0

這將適合整個屏幕,而不是OP想要的。 – Androiderson

+0

據我所知,「RelativeLayout」的大小對應於屏幕大小 - 它在寬度和高度屬性上有'match_parent'。 – g00dy

0

首先,我相信有一個錯誤在XML兩件事情:

RelativeLayout不使用方向,你應該刪除該方向

這是你的完整xml文件嗎?如果是這樣,則RelativeLayout中的alignParentLeft不會執行任何操作,因爲如果它的父親是RelativeLayout,那麼它就是您使用的屬性。

fill_parent是match_parent的棄用名稱,您應該使用該名稱。

我也相信你所擁有的代碼會將圖像放在屏幕的中心,使得任何東西在雙方均勻分佈,而不是你所說的。

如果你只是想你可以在RelativeLayout的使用android:background="@drawable/bg4"背景

編輯:更改的ImageView的的LayoutParams爲wrap_content高度和寬度,並去除scaleType產品總數可能使其像你希望它是。

1

使用此code.it的輸出,如你所願。
代碼,如果你給了imageview的寬度填充母

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

    <RelativeLayout 
     android:id="@+id/relativeLayoutMain" 
     android:layout_width="150dp" 
     android:background="@android:color/background_dark" 
     android:layout_height="150dp" > 

     <ImageView 
      android:id="@+id/bgImage" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:scaleType="centerCrop" 
      android:src="@drawable/ic_launcher" /> 
    </RelativeLayout> 

</RelativeLayout> 

一個建議則是根據主容器包含整個空間。 Best Luck