2011-09-30 70 views
0

我有一個RelativeLayout,我需要在它的中間有一個黑色背景和一個小圖像。我用這個代碼:RelativeLayout background image + color

<RelativeLayout 
    android:id="@+id/bottomBox" 
    android:layout_weight="4" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bottom_box_back" 
    android:layout_marginTop="4dp" 
    /> 

其中@drawable/bottom_box_back是:

<bitmap 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:src="@drawable/logo_icon" 
android:tileMode="disabled" 
android:gravity="center" 
android:filter="true" 
/> 

這樣我有圖像居中。但背景仍然是灰色的主窗口。我需要一個黑色的背影遍佈RelativeLayout空間。有什麼建議麼?

+2

如何使用層列表可繪製資源? 1.將純色可繪製爲底層2.將位圖層放在底層 – kingori

+0

上面!非常感謝! –

回答

1

我會讓你的RelativeLayout的背景上的黑色,然後把圖像轉換成一個ImageView的和中心在RelativeLayout的中間的那個,所以:

<RelativeLayout 
    android:background="@color/yourBlackColor"> 

    <ImageView 
     android:layout_centerInParent="true"> 
    </ImageView> 

</RelativeLayout> 

希望這有助於你

0

另一種選擇是使用9補丁圖像。實際上,9貼片圖像上可以有多個可拉伸區域,因此您只需將想要居中的圖像置於中間區域,然後將其作爲背景色以黑色包圍即可。然後,在聲明可拉伸區域時,請選擇圖像上方,下方和左側和右側的區域作爲可拉伸區域。當應用程序拉伸區域時,它會均勻拉伸它們,只要可拉伸區域的大小相同,圖像就會保持在中心位置。

我知道這已經有一個公認的答案,但我認爲這是完成同樣的事情,而無需添加另一個圖像視圖的佈局的另一種乾淨的方式。