2011-08-17 95 views
3

我想在我的應用程序中添加一個背景圖片到listActivity。但是,使用背景圖像的代碼顯示在每一行上而不是整個列表中。在Android中添加背景圖像到列表視圖?

這是XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:padding="8px" > 

<ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:src="@drawable/qbg1"/> 

<TextView 
    android:id="@+id/employeeID" 
    android:layout_width="50px" 
    android:layout_marginRight="20px" 
    android:layout_height="wrap_content" 
    android:textColor="@color/textColor"/> 

<TextView 
    android:id="@+id/employeeName" 
    android:layout_width="wrap_content" 
    android:layout_marginRight="10px" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/employeeID" 
    android:textColor="@color/textColor"/> 


</RelativeLayout> 
<ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:src="@drawable/qbg1"/> 

是什麼背景圖像添加到活動的最佳方法?我寧願在XML文件中執行,而不是通過編程方式。

+0

[ListView的背景圖片]的可能重複(http://stackoverflow.com/questions/5137898/listview-with-background-image ) – Snicolas

回答

9

我解決了這個問題,將圖像背景添加到主視圖,然後在該視圖內添加listView。

所以這是我的main.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:paddingTop="4px" 
android:background="@drawable/qbg"> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:drawSelectorOnTop="false" 
      android:cacheColorHint="#00000000"/> //This is to fix disappearing background issue 
</RelativeLayout> 

,這是我listView.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:padding="8px" > 

<TextView 
android:id="@+id/employeeID" 
android:layout_width="50px" 
android:layout_marginRight="20px" 
android:layout_height="wrap_content" 
android:textColor="@color/textColor"/> 

<TextView 
android:id="@+id/employeeName" 
android:layout_width="wrap_content" 
android:layout_marginRight="10px" 
android:layout_height="wrap_content" 
android:layout_toRightOf="@id/employeeID" 
android:textColor="@color/textColor"/> 

</RelativeLayout> 

希望這能對別人有益的。

2

我已經使用類似的東西來獲得這種效果。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/paper" 
> 
<ExpandableListView 
android:id="@+id/listView" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:fadingEdge="vertical" 
android:fadingEdgeLength="10dip" 
android:scrollingCache="false" 


> 
</ExpandableListView> 
</RelativeLayout> 

我記得有來調整一些設置,讓這個當名單被然而滾動的背景留下可見。

+0

我已經用不同的方法解決了這個問題。我打算在7小時後發佈它,因爲SO在這段時間之前不讓我發佈答案:( –

2

你有沒有嘗試設置在代碼中的圖像,試試這個getListView().setBackgroundResource(R.drawable.matchback);