简体   繁体   中英

Adding background image to listview in Android?

I am trying to add a background image to a listActivity in my app. However, with the code that I am using the background image displays on every single row rather than for the whole list.

This is the xml file:

<?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"/>

what is the best approach to add a background image to the activity? I would prefer to do in the XML file rather than programmatically.

I solved the issue by adding the image background to to the main view and then add the listView inside that view.

So this is my main.xml file:

<?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>

and this is my 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>

Hope this will beneficial for other people.

Did you try to set the image in the code, try this getListView().setBackgroundResource(R.drawable.matchback);

I have used something like this to get that effect.

<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>

I remember having to tweak some of the settings to make it so that the background stayed visible while the list was being scrolled however.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM