2016-03-08 639 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在另一个布局。