【问题标题】:setup background color in listview在列表视图中设置背景颜色
【发布时间】:2011-11-18 18:17:17
【问题描述】:

如何让我的整个背景页变白? 我有一个列表视图并尝试在 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);
        }
    });
}

}

【问题讨论】:

  • 现在到底发生了什么?能发个截图吗?

标签: android xml listview background-color


【解决方案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>

【讨论】:

  • @color 属性应该在 /values/colors.xml 中定义
【解决方案2】:

最简单的方法是set a theme。在您的 manifest.xml 文件中,只需将此属性添加到您的活动中:

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

【讨论】:

    【解决方案3】:

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

    首先,在您的活动中创建一个 ListView 变量(在 onCreate 上方):

    ListView LV = null;
    

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

    LV = getListView();
    

    或者假设 mylist id 在 XML 中设置为列表视图,则此方法可行:

    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" 似乎不起作用。知道为什么会出现这种行为吗?
    • @color/white 之类的东西可以使用预定义的颜色,或者您可以在 strings.xml 中制作自己的颜色,请参见此处:stackoverflow.com/questions/2748830/…
    【解决方案4】:

    如果你想在索引“0”处设置背景颜色,你应该试试这个,它很好用,我使用了这个代码。

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

    【讨论】:

      【解决方案5】:

      您需要在项目的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"/>
      

      【讨论】:

        猜你喜欢
        • 2012-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多