在ListView中设置背景颜色

6

如何将整个背景页变为白色? 我有一个列表视图,并尝试在xml中将背景颜色设置为白色,但它没有起作用。 这是我的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>

唯一变白的是这个:
<?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>

这是我的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);
        }
    });
}

}


1
现在到底发生了什么?你能发一张截图吗? - Moss
5个回答

15

这有一个非常简单的方法。

首先,在你的活动中创建一个ListView变量(在onCreate之前):

ListView LV = null;

如果您扩展了ListActivity,则可以在onCreate中设置变量,方法如下:

LV = getListView();

或者,假设XML中的mylist id已设置为listview,则可以使用以下代码:

LV = (ListView) findViewById(R.id.mylist);

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

LV.setBackgroundColor(Color.WHITE);
或者将其设置为可绘制对象:
int r = getResources().getIdentifier("subtle_white_gradient", "drawable", "com.my.package");
LV.setBackgroundResource(r);

设置编程方式可以工作,但是使用布局XML方式android:background="@color/background"似乎不起作用。为什么会出现这种情况?有什么想法吗? - Akh
类似@color/white这样的东西将与预定义的颜色一起使用,或者您可以在strings.xml中创建自己的颜色,请参见此处:https://dev59.com/7nE85IYBdhLWcg3wgDlI - Mischa

5
如果您想在索引为“0”的位置设置背景颜色,您应该尝试这个代码,它很有效,我已经使用过了。
list.post(new Runnable() {

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

3
您需要在项目的res/value文件夹中创建color.xml文件。 color.xml是...
 <?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="orange">#ff5500</color>
 <color name="white">#ffffff</color>
 <color name="transparent">#00000000</color>
 <color name="black">#000000</color>
 <color name="gray">#999999</color>
 <color name="blue">#0066cc</color>

</resources>

现在在您的Lisiview中将此颜色称为:
<ListView  
        android:id="@+id/list" 
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"
        android:background="@color/blue"/>

1

最简单的方法是设置主题。在您的manifest.xml文件中,只需在您的活动下添加此属性:

android:theme="@android:style/Theme.Light"

-1

在 XML 中尝试这个:

<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"
            android:background="@color/background" >
        </ListView>

创建一个名为colors.xml的XML文件,定义你想要的颜色:
<color name="background">#CDB38B </string>

1
@color属性应该在/values/colors.xml文件中定义。 - Akh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接