2011-03-09 70 views
7

我有機器人9的貼劑圖形不縮放圖像視圖

<LinearLayout android:id="@+id/LinearLayout01" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <ImageView android:layout_height="wrap_content" 
     android:layout_width="wrap_content" android:id="@+id/ImageView01" 
     android:src="@drawable/phone80"></ImageView> 
    <ImageView android:id="@+id/ImageView02" 
     android:layout_height="wrap_content" android:src="@drawable/underline" 
     android:layout_width="fill_parent" android:layout_gravity="fill_vertical|fill_horizontal"></ImageView> 
</LinearLayout> 

第一圖像是其不應當比例在所有的固定尺寸的圖像。它的權利應該水平伸縮到最大的空間。源指向有效的.9.png文件。不幸的是,它總是隻顯示其原始大小。我必須設置哪些屬性? ImageView是否是正確的對象?

感謝, A.

回答

22

使用android:background代替。如果只是下劃線,則可以使用View而不是ImageView。至於填充屏幕的其餘部分,您可能需要使用TableLayout

此外,請確保您的9補丁是正確的。 1px邊必須是黑色的。 #000

+0

剛剛得到一個downvote ...很想一些反饋,以便其他人可以受益。 – trgraglia 2013-04-18 07:24:02

+0

將圖像添加到背景而不是src意味着另一層內容會降低性能。是不是我downvote,但我也想要能夠使用填充圖像,並在加載時與最終圖像交換它;沒有兩個圖像堆疊。 – radley 2013-06-27 20:44:16

+0

是的,並非如此。您可以將背景添加到根線性佈局,然後您可以對其進行填充或格式化,這意味着內容將成爲第二層並且更易於維護......兩者都很棒,但取決於用例。感謝您的評論! – trgraglia 2013-07-01 08:06:02

50

只需將android:scaleType="fitXY"添加到您的ImageView即可。例如,對於myimage.9.png:

<ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:src="@drawable/myimage" 
    android:scaleType="fitXY" /> 
+1

這是正確的。如果沒有fitXY 9補丁將無法兌現,圖像會像平常一樣延伸。 – user123321 2013-03-26 02:26:06

+0

感謝您的支持。 =) – DarwinLouis 2015-05-29 11:37:39

+0

肯定是正確的答案,因爲當ImageView處於RelativeLayout內部以及相對於其他視圖時,android:background不會被考慮。 – 2015-09-05 14:55:20

1

同時檢查以確保在兩側的刻度線一路不延伸到圖像的角落。我知道那裏有黑色像素並沒有什麼意義,但我認爲Android會忽略它們,但它實際上阻止了縮放。它還會在輸出窗口中顯示「ERROR:9-patch image xxx.9.png malformed」消息,但您可能沒有注意到這一點。我一拿出角落中的像素,就開始爲我工作。

0

嘗試使用setImageResource(9patchResourceID),而不是setImageBitmap或setImageBackground和工程:)