2016-03-08 600 views
0

所以,我想將圖片放在我的屏幕背景中。我的問題是,如果我只是將圖像添加到「背景」,它將填滿所有的屏幕,並且我的圖像被損壞。我可以把它作爲一個ImageView,但問題是我在同一個屏幕上有一個expandableListView,並且我希望圖像留在後面,作爲背景。 任何想法如何做到這一點? 可以有2個解決方案:將其作爲imagemView放入列表中。或添加爲背景,但調整它的大小。我該怎麼做?Android Studio - 更改背景圖片大小

這裏是發生了什麼:

enter image description here

他,如果我把圖像作爲ImageView的會發生什麼:

enter image description here

+0

背景會自動拉伸以填充視圖。如果你不想這樣做,你需要使用一些可以給你更多控制的東西,比如可以設置縮放類型的ImageView。 –

+0

如果你不想在ListView背後使用'ImageView'創建一個自定義'Drawable'類,並將它與'listView.setBackground(Drawable)'一起使用' – pskink

+0

@GabeSechan 但是,如果我使用imageView,我會產生另一個問題。圖像將保留在我的expandableListView前面。看到我放在頂部的例子 –

回答

1

是的,這是很容易實現的,相對佈局

<RelativeLayout ... > 
    <ImageView ... 
      android:layout_alignParentTop="true" 
      android:scaleType="centerCrop「 /> 
    <ListView ... 
      android:layout_alignParentTop="true「 /> 
</RelativeLayout> 

使用相對佈局z-index由您聲明標籤的順序決定,因此在xml底部的東西位於距離頂部較近的東西前面,當它們處於相同位置時。

+0

謝謝!我不知道這個順序..:b –

0

使用的FrameLayout(寫在上面裏面的FrameLayout視圖將出現在頂部等)

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

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    ></ListView> 


<ImageView 
    android:layout_width="your_width_in_dp" 
    android:layout_height="your_height_in_dp" 
    /> 

</FrameLayout> 

您也可以放置的FrameLayout在另一個佈局。