2011-08-12 41 views
11

您能幫我配置我的佈局嗎?我不達到什麼我在尋找:使ImageView(及其src)適合佈局寬度並使其高度成比例

我已經把ImageView的背景爲紅色解釋什麼,我想:

圖像的寬度大於佈局寬度。我希望圖像按比例保持其縱橫比,直到達到第一個圖像的方面(我用photoshop手動修改它),但是當設置width =「fill_parent」時,結果是第二個圖像,並將寬度和高度都設置爲「 fill_parent「結果是第三個圖像。

我試圖用ScaleType結合起來,但沒有成功

我如何能實現第一個選項?由於

enter image description here

回答

2

使用fit_center屬性到您的ImageView的標籤。

<ImageView ... android:scaleType="fitCenter" .. /> 
+0

感謝您的快速回答。你的意思是android:scaleType =「fitCenter」吧?與該選項和兩個維度fill_parent結果是類似於圖像3(圖像在中間)。例如,height =「wrap_content」的結果是圖像2的中間爲水平方向 – Addev

+0

對於兩個參數都使用「wrap_content」,如果使用的是LinearLayout,則添加android:layout_weight =「1」 –

14

使用android:adjustViewBounds=true以XML爲ATTR ImageView


這個問題是通過擴展的ImageView類在這裏解決。

Android ImageView adjusting parent's height and fitting width


我創建了一個示例。這個對我有用。這是xml佈局文件。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ImageView android:src="@drawable/hydrangeas" 
     android:layout_height="wrap_content" android:id="@+id/imageView1" 
     android:scaleType="centerInside" 
     android:adjustViewBounds="true" 
     android:background="@android:color/white" 
     android:layout_width="wrap_content"/> 

</LinearLayout> 

它也適用於fitCenter。看看快照。
With centerInside and adjustViewBounds="true"

+0

我真的不明白adjustViewBounds如何工作,但我沒有達到我的目標,你可以解釋一下如何做到這一點? – Addev

+0

嘗試使用scaletype = centerInside以及它。 – Ronnie

+1

嗨,這對我來說也不起作用。較小的圖像不會放大以適應父視圖,但可以留下som邊框。 –

相關問題