2011-01-21 41 views
3

我有一個很大的png,我想在不同的佈局上使用它作爲背景,但抵消了它,以便我可以顯示不同的部分(很像您可以在CSS中),最好在xml中。Android佈局 - 在xml中抵消背景圖片

我該活動主要佈局包含以下XML:

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@layout/bg1"> 

佈局BG1由以下的xml:

<?xml version="1.0" encoding="utf-8"?> 
<bitmap 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:src="@drawable/big_image" 
android:layout_marginTop="50sp" 
android:paddingTop="50sp" 
android:gravity="top|left" /> 

重力屬性按預期工作,但邊距和補被忽略,大概是因爲我正在處理位圖對象而不是佈局。我想要做的是將這些設置爲負數,以便只顯示部分圖片。我試過使用一個形狀,但只包裝內容,而我需要填充整個背景。

任何建議將受到感謝。謝謝。

+0

解決與以下,確保ImageView的是在XML的頂部,使得它是所有其他控制背後: 2011-01-27 16:05:53

回答

4

我已經使用了負值的上邊距值的ImageView。 ImageView首先在佈局中聲明,以便它在控件堆棧中最低,並且將呈現在其他控件的後面。

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ImageView 
     android:id="@+id/bigLogo" 
     android:src="@drawable/big_logo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true"  
     android:layout_marginTop="-100px" /> 
</RelativeLayout> 
2

您可以使用InsetDrawable(它從XML工作)向其他drawable添加額外的填充。

+1

我改變了原始文章中bg1.xml的佈局,與insetTop設置。但是,佈局的整個內容不僅僅是背景可繪製的。我期待背景被當作網頁這樣的「水印」來對待,而且主要內容會受到影響。也許我應該只使用定位的ImageView? – 2011-01-24 17:32:13