开发者

set a background for a listview

I have four tabs that hold four listviews, I want to set a background for each list view but whenever I try to add the background it puts the image in ea开发者_Python百科ch cell of the listview instead of behind the list.

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

android:background="@drawable/pre"

android:layout_width="fill_parent"
android:layout_height="fill_parent"

android:padding="10dp"

android:textSize="21sp">

</TextView>

I've realised that this is beacuse I've tried to add the background in a textview so it adds the image in each cell in the listview, so I have tried to add a linearlayout, listview and a imageview and put the background there but it force closes. I think this is becuse the tabhost uses main.xml to draw the main page and it conflicts, so I even tried to add the listview there still it nforce closes, it will only work if I have a textview only, howe can I add a background to each listview, below is the listview code;

public class prem extends ListActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    // Create an array of Strings, that will be put to our ListActivity
    String[] names = new String[] { "Pre"};
    ListView lv = getListView();    
    lv.setCacheColorHint(00000000);    
    lv.setAdapter(new ArrayAdapter<String>(this,
            R.layout.list_item, names));

}


Okay, your XML layout files are going to be used in the setContentView() method. You haven't posted the code for your activity which includes the TabHost, but I'll assume you're using the default setContentView(R.layout.main);. If you're NOT using setContentView() (in the case of your ListActivity), adding a ListView to an XML file isn't going to change anything, because it's never being used.

You are correct in that you're having the issue because you're setting the background of the TextView. Since you're using a ListActivity, you'll need to set the ListView's background using code. ListView is a subclass of View, so you can use the methods from the View class to set your background resource for the ListView.

For example:

ListView listView = getListView();

//set background to color
listView.setBackgroundColor(#FF888888);

//set background to Drawable
listView.setBackgroundDrawable(myDrawable);

//set background to Resource
listView.setBackgroundResouce(R.id.my_res_id);


As per JonniBravo, adding....

If you have an XML layout file that you are using to set the view by using setContentView() in your activity, you can have a ListView defined inside that.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lists_view"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView 
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/paper_frame" />
</RelativeLayout>

The activity can "find it" using

lv = getListView();

As shown in the sample above, you can set the background for the ListView with the "background" attribute. As usual, this can be a valid drawable (e.g. a color, or an image). In my example it happens to be a 9.patch image that is stretched by Android to enclose the list, providing a frame.

Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜