如何将背景设置为列表视图?

How to set background to listview?

我想将背景设置为列表视图,这张照片显示了目的

photo one : this is the xml file , show background ! 但是 photo two see!! when run don't show background

这是 java 文件中的代码

public class a extends ListActivity {

String[] listItems = {"سه‌ره‌تای ئیمان ـ باوه‌ڕ ـ وتنی: (لا اله الا الله)یه‌","فه‌رمانم پێكراوه‌ بجه‌نگم له‌گه‌ڵ خه‌ڵكیدا هه‌تا ده‌ڵێن (لا اله الا الله)"};

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setListAdapter(new ArrayAdapter<String>(a.this,
android.R.layout.simple_list_item_1, listItems));

}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);

if (position == 0) {
Intent intent = new Intent(this, a_a_activity.class);
startActivity(intent);
} else if (position == 1) {
Intent intent = new Intent(this, a_a_activity.class);
startActivity(intent);
}

您可以使用 getListView()

ListActivity 中访问 ListView
getListView().setBackgroundColor(0xFFDED2BE);

很简单。通过 getListView() 你可以做到。

喜欢这个代码:

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class a extends ListActivity {

String[] listItems = {
        "سه‌ره‌تای ئیمان ـ باوه‌ڕ ـ وتنی: (لا اله الا الله)یه‌",
        "فه‌رمانم پێكراوه‌ بجه‌نگم له‌گه‌ڵ خه‌ڵكیدا هه‌تا ده‌ڵێن (لا اله الا الله)" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setListAdapter(new ArrayAdapter<String>(a.this,
            android.R.layout.simple_list_item_1, listItems));
    // Get Active listView
    getListView().setBackground(R.drawable.your_background);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    if (position == 0) {
        // Intent intent = new Intent(this, a_a_activity.class);
        // startActivity(intent);
    } else if (position == 1) {
        // Intent intent = new Intent(this, a_a_activity.class);
        // startActivity(intent);
    }
}
}

当您使用 ListActivity 时,您可以通过调用方法获得您的 ListView

ListView list = getListView(); // OR you can do
ListView list = (ListView)findViewById(android.R.id.list);  //consider the android prefix..

您可以使用 getListView() 方法完成此操作。

public class a extends ListActivity {

String[] listItems = {"سه‌ره‌تای ئیمان ـ باوه‌ڕ ـ وتنی: (لا اله الا الله)یه‌","فه‌رمانم پێكراوه‌ بجه‌نگم له‌گه‌ڵ خه‌ڵكیدا هه‌تا ده‌ڵێن (لا اله الا الله)"};

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setListAdapter(new ArrayAdapter<String>(a.this,
android.R.layout.simple_list_item_1, listItems));

//getListView.setBackground(getDrawable(R.drawable.your_background_image));


}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);

if (position == 0) {
Intent intent = new Intent(this, a_a_activity.class);
startActivity(intent);
} else if (position == 1) {
Intent intent = new Intent(this, a_a_activity.class);
startActivity(intent);
}
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setListAdapter(new ArrayAdapter<String>(a.this,
            android.R.layout.simple_list_item_1, listItems));
    // Get Active listView
  getListView().setBackgroundResource(R.drawable.background_file_name);

}