2013-10-14 46 views
5

我需要一些幫助,使得背景圖像在線性佈局中以垂直和水平方向伸展或縮放。但是,每次嘗試設置背景時,都不會將圖像拉伸以適合屏幕。我不關心保持寬高比。如何在Android線性佈局中拉伸/縮放背景圖像?

下面是我在一個測試XML的時刻了:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/title" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/background"> 
</LinearLayout> 

它看起來像這樣的預覽(在遊戲中):

enter image description here

如果我改變layout_height fill_parent我得到一個重複的背景:

enter image description here 我敢肯定,解決方案是非常基本的,但由於某種原因,我錯過了它。任何幫助,將不勝感激

感謝

+0

這個問題似乎與平板設備來了......嘗試使用更大的圖像並將寬度和高度設置爲'match_parent' – MikeKeepsOnShine

+0

希望這裏有更多答案。 –

+0

你是如何測試這樣的所有屏幕的? – Sami

回答

6

不知道什麼可用的背景佈局選項可用於佈局的背景屬性,但你總是可以只是把一個ImageView的作爲第一要素與最大寬度外的FrameLayout和身高。然後,您放入佈局的任何其他內容都將繪製在此圖像的頂部。當然,你可以只加載你想要的縮放類型;比如fitXY,或許是你想要達到的目標。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ImageView 
    android:id="@+id/background" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:contentDescription="@string/backgroundDescription" 
    android:scaleType="centerCrop"> 
</ImageView> 
... 
4

這裏是重要android:scaleType參數具有可能的值之一:

  • 矩陣
  • fitXY
  • fitStart
  • fitCenter
  • fitEnd
  • 中心
  • centerCrop
  • centerInside

,如果你不介意的圖像時,它是必要的(不同的比例比你的圖像畫面)被croped,你不希望圖像變形,我會建議centerCrop。 如果重要的是圖像沒有被裁剪,並且如果它是歪斜的,那麼您不需要打印,fitXY將會完成這項工作。

希望這有助於清除和事情有點...

0

您可以使用scaleType與centerCrop

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scaleType="centerCrop" 
    android:src="@drawable/background" /> 

+0

我認爲在這種情況下fitXY會更好 – Chaosit