如何设置listview的背景?

时间:2015-01-21 22:07:43

标签: android listview background

我想将背景设置为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);
}

4 个答案:

答案 0 :(得分:0)

您可以使用ListActivity

getListView()内访问ListView
getListView().setBackgroundColor(0xFFDED2BE);

答案 1 :(得分:0)

非常简单。通过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..

答案 2 :(得分:0)

您可以使用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);
}

答案 3 :(得分:0)

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

}