2011-10-05 131 views
0

我在Activity类中使用了ListView。我没有为Lis​​tView使用任何XML代码。现在我无法更改listView中文本项目的颜色。如何更改列表视图中列表项的颜色

我改变了ListView的背景颜色,但无法改变列表项的颜色。我从互联网上获得了一些链接,但无法弄清楚。任何人都可以帮助我用一些代码做到这一点?

我Activity类看起来是这样的:

ListView listView; 
     // Create an array of Strings, that will be put to our ListActivity 
     String[] names = new String[] { "India", "Malaysia" }; 
     TextView tv = new TextView(getApplicationContext()); 
     tv.setText("Select Country"); 
     tv.setTextColor(012); 

     listView = getListView(); 
     listView.addHeaderView(tv); 

     listView.setCacheColorHint(Color.rgb(36, 33, 32)); 
     listView.setBackgroundColor(Color.rgb(225, 243, 253)); 
     this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,names)); 

    } 

以上,tv.setTextColor设置列表项的标题。它不是因为它要求我传递一个整数值。我可以传递什么样的整数值用于颜色? 任何人都可以建议更改列表项的颜色的代码?

回答

1

对于有色的listitem,您需要自定义它。为此,准备一个xml文件:

custom_listitem.xml

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


    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" 
      android:textColor="#FFFFFF" 
      android:id="@+id/list_item" 
      android:background="#FF0000" <!-- for red color --> 
      />  

</LinearLayout> 

现在,你有你的适配器来使用这个喜欢 -

listView.setListAdapter(new ArrayAdapter<String>(this,R.layout.custom_item,R.id.list_item,names)); 

是的,你可以使用Color.REDColor.YELLOW etc.for默认的颜色,我们可以使用"#3C3C3C"(同时使用xml)或Color.parseColor("3C3C3C")(while usi ng编程)除默认颜色以外的任何颜色。

0

您需要创建一个CustomListAdapter。

private class CustomListAdapter extends ArrayAdapter { 

    private Context mContext; 
    private int id; 
    private List <String>items ; 

    public CustomListAdapter(Context context, int textViewResourceId , List<String> list) 
    { 
     super(context, textViewResourceId, list);   
     mContext = context; 
     id = textViewResourceId; 
     items = list ; 
    } 

    @Override 
    public View getView(int position, View v, ViewGroup parent) 
    { 
     View mView = v ; 
     if(mView == null){ 
      LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      mView = vi.inflate(id, null); 
     } 

     TextView text = (TextView) mView.findViewById(R.id.textView); 

     if(items.get(position) != null) 
     { 
      text.setTextColor(Color.WHITE); 
      text.setText(items.get(position)); 
      text.setBackgroundColor(Color.RED); 
      int color = Color.argb(200, 255, 64, 64); 
       text.setBackgroundColor(color); 

     } 

     return mView; 
    } 

} 

列表项看起来像这样(custom_list.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView" 
    android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/> 
</LinearLayout> 

使用TextView的API对你的文字装点自己的喜好

,你将使用它像这样

listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList); 
mListView.setAdapter(listAdapter); 
相关问题