2011-10-05 96 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); 
相關問題