在listview中设置背景颜色

Fab*_*ian 4 xml android listview background-color

如何将整个背景页设为白色?我有一个listview并试图在xml中设置backgroundcolor白色,但它没有用.这些是我的xmls:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ListView 
android:id="@+id/list" android:layout_width="fill_parent" 
android:clickable="true" android:layout_height="fill_parent"></ListView>
Run Code Online (Sandbox Code Playgroud)

唯一真正变白的是:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/naam" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
Run Code Online (Sandbox Code Playgroud)

这是我的java代码:

public class Contactenlijst extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final HashMap<Integer, Personeel> personeelmap = new HashMap<Integer, Personeel>();
    ArrayList<String> list = new ArrayList<String>();
    // Get the data (see above)

    JSONObject json = Database
            .getJSONfromURL("http://fabian.nostradamus.nu/Android/getcontactinfo.php");

    try {
        JSONArray contactinfo = json.getJSONArray("contactlijst");
        // Loop the Array
        for (int i = 0; i < contactinfo.length(); i++) {

            JSONObject e = contactinfo.getJSONObject(i);
            Personeel p = new Personeel(
                    Integer.parseInt(e.getString("id")),
                    e.getString("staff_name"),
                    e.getString("staff_lastname"),
                    e.getString("staff_dateofbirth"),
                    e.getString("staff_address"),
                    e.getString("staff_address_postal"),
                    e.getString("staff_address_city"),
                    e.getString("staff_mobile"));
            personeelmap.put(Integer.parseInt(e.getString("id")), p);
            list.add(p.toString());
        }

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }

    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, list));
    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    // onclick stuur array naar contactinfo

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            String text = ((TextView) view).getText().toString();
            Intent i = new Intent(Contactenlijst.this, Contactinfo.class);
            String uittekst[] = text.split(" ");
            String vnaam = uittekst[0].toString();

            ArrayList<String> al = new ArrayList<String>();
            int a = personeelmap.size();

            a = a + 1;
            for (int c = 1; c < a; c++) {

                if (personeelmap.get(c).getStaff_name().toString()
                        .equals(vnaam)) {
                    al.add(personeelmap.get(c).getStaff_name());
                    al.add(personeelmap.get(c).getStaff_lastname());
                    al.add(personeelmap.get(c).getDateofbirth());
                    al.add(personeelmap.get(c).getStaff_address());
                    al.add(personeelmap.get(c).getStaff_address_postal());
                    al.add(personeelmap.get(c).getStaff_address_city());
                    al.add(personeelmap.get(c).getStaff_mobile());
                }
                ;
            }

            i.putStringArrayListExtra("array", al);
            startActivity(i);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

}

Mis*_*cha 14

有一种非常简单的方法可以做到这一点.

首先,在您的活动中创建一个ListView变量(在o​​nCreate上面):

ListView LV = null;
Run Code Online (Sandbox Code Playgroud)

然后在onCreate中设置变量,你可以这样做,假设你扩展了ListActivity:

LV = getListView();
Run Code Online (Sandbox Code Playgroud)

或者这适用于假设mylist id在XML中设置为listview:

LV = (ListView) findViewById(R.id.mylist);
Run Code Online (Sandbox Code Playgroud)

最后,将背景设置为一种颜色:

LV.setBackgroundColor(Color.WHITE);
Run Code Online (Sandbox Code Playgroud)

或将其设置为drawable:

int r = getResources().getIdentifier("subtle_white_gradient", "drawable", "com.my.package");
LV.setBackgroundResource(r);
Run Code Online (Sandbox Code Playgroud)


San*_*ral 5

如果你想在索引'0'设置背景颜色,你应该试试这个它工作得很好我使用了这个代码.

list.post(new Runnable() {

    @Override
    public void run() {
        list.setSelected(true);
        list.setBackgroundColor(Color.BLACK);
        list.getChildAt(0).setBackgroundColor(Color.BLUE);          
    }
});
Run Code Online (Sandbox Code Playgroud)