2012-07-12 123 views
2

我想在背景圖片中放置一個RelativeLayout。背景圖像中有一些物體,如椅子,桌子,一些卡片等。Android。佈局背景延伸

android:background="@drawable/remy_menu_background"但該死的東西是根據屏幕延伸的,卡片和桌子看起來很舒展。

我應該如何去做這件事,所以它在任何屏幕上看起來都不錯?

+0

使用9個補丁工具 – Houcine 2012-07-12 09:06:50

+0

發佈烏爾佈局的xml文件 – Aamirkhan 2012-07-12 09:23:49

+0

可以是相同的情況:http://stackoverflow.com/questions/8202647/setting-a-background-image-to-a-view-stretches -my-view – MKJParekh 2012-07-12 09:27:19

回答

9

您可以使用一個使用標籤的XML繪製禁用拉伸:

<bitmap android:src="@drawable/background" android:gravity="center" /> 
+3

hmm。我試過這個,但它使應用程序崩潰。在圖形佈局選項卡的菜單xml中,它表示 「無法找到以下類: - 位圖(修復構建路徑,編輯XML)」關於我在做什麼錯誤的任何想法? – AndreiBogdan 2012-07-12 09:22:45

+2

添加android:scaleType =「fitCenter」或「中心」 – Houcine 2012-07-12 09:29:24

+1

不錯的答案+1。 – 2012-07-12 09:32:46

1

你可以在這樣的情況下使用.9.png圖像,並可以指定圖像的面積應舒展而哪些不。您可以使用android-sdk中'tools'文件夾下的「draw9patch」工具將任何png轉換爲.9.png。

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

<ImageView 
    android:id="@+id/main_backgroundImage" 
    android:layout_width="match_parent" // comment: stretches picture in the width if too small. 
              use "wrap_content" does not stretch, but leaves space 
    android:layout_height="match_parent" // in my case I always want the height filled 
    android:layout_alignParentTop="true" 
    android:scaleType="centerCrop" // will crop picture left and right , so it fits in height and keeps aspect ratio 
    android:contentDescription="@string/image" 
    android:src="@drawable/your_image" /> 

<LinearLayout 
    android:id="@+id/main_root" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
</LinearLayout>