android - 在 ListView 中设置背景颜色

标签 android xml listview background-color

如何让我的整个背景页面变白? 我有一个 ListView 并尝试在 xml 中将背景颜色设置为白色但是 它没有用。 这些是我的 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"></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);
        }
    });
}

最佳答案

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

首先,在您的 Activity 中创建一个 ListView 变量(在 onCreate 之上):

ListView LV = null;

然后在 onCreate 中设置变量,假设您扩展了 ListActivity:

LV = getListView();

或者假设 mylist id 在 XML 中设置为 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);

关于android - 在 ListView 中设置背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7446241/

相关文章:

java - Java中的音频指纹识别系统

android - 从 Firebase 检索位置并将标记放在 google map api for android

android - 按钮不透明度/透明度

xml - 从 mulesoft 中的 xml 列表中提取特定的 xml 节点

java - JAXB 继承冲突 - 在子类上重新注释

Android fragment 布局帮助

android - 在 Play 商店分阶段推出中,该百分比仅适用于现有用户还是新用户和现有用户?

Android:如何测量 ListView 的总高度

android - 如何从 ArrayAdapter 中的 onClickListener 重新加载 Activity

c# - 当鼠标悬停在内部 ListView 上时,ListView 内的 WPF ListView 不滚动