2013-05-20 78 views
0

是否有一種簡單的方法來更改特定列表視圖項的字體顏色?我有一個ArrayAdapter項目,我傳遞給ListView中的setAdapter()。我只想改變列表中特定元素的顏色。如何更改列表視圖項的字體顏色

這是我到目前爲止的代碼

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked); 

    ListView lv = (ListView) findViewById(R.id.listView1); 
    lv.setAdapter(adapter); 
+0

請發表您的代碼。所以它很容易幫助! – Oam

回答

0

在你listview.xml,你需要在你的元素做到這一點(在這種情況下,它是一個TextView):

<TextView 
     android:textColor="@color/black"/> 

在文件夾,您需要創建一個colors.xml文件。到文件你把:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<color name="black">#000000</color> 
</resources> 

這是一個HTML顏色

我希望我已經幫你

0

在適配器中的代碼

public View getView(int position, View convertView, ViewGroup parent) 
{ 
    MyView view; 
    if(convertView == null) 
    { 
     // Get the item to draw 
     Item i = getItem(position); 

     // Create its view 
     view = new MyView(...); 

     if(i.isSpecific) 
     { 
      // If its your specific item change the bg color 
      view.setBackground(color); 
     } 
    } 
    else 
    { 
     // Don't create a view if it's already exist 
     view = convertView; 
    } 
return view; 

}

+0

所以我必須創建一個擴展ArrayAdapter的Adapter類? –

+0

是的,並重寫getView方法。 – pierre

+0

所以當我創建一個新的視圖時,我發送給構造函數的是什麼上下文? –

相關問題