2012-05-29 21 views
1

我在活動中有一個ExpandableListView。該代碼是:在適配器如何設置ExpandableListView的子文本顏色

final ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView1); 
    expandableListView.setDivider(null); 
    expandableListView.setCacheColorHint(0); 
    expandableListView.setGroupIndicator(null); 
    final EEInterviewExpandableListAdapter adapter = new EEInterviewExpandableListAdapter(this); 
    expandableListView.setAdapter(adapter); 

及相關代碼:

public View getChildView(int groupPosition, int childPosition, 
     boolean isLastChild, View convertView, ViewGroup parent) { 
    String string = pageArray.get(groupPosition).get(childPosition); 
    boolean highlight=false; 
    return getChildGenericView(string,highlight);  
} 
public TextView getChildGenericView(String string,boolean highlight)  
{  
    // Layout parameters for the ExpandableListView  
    AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams( 
      ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    TextView text = new TextView(activity);  
    text.setLayoutParams(layoutParams);  
    // Center the text vertically  
    text.setGravity(Gravity.CENTER | Gravity.LEFT);  
    // Set the text starting position  
    text.setPadding(0, 2, 0, 2);  
    text.setText(string.trim());  
    text.setBackgroundResource(R.drawable.childsep); 
    if(highlight) 
     text.setTextColor(R.color.expchdhighlightcolor); 
    else 
     text.setTextColor(R.color.expchdcolor); 
    text.setTextSize(EEEnv.EEfontSize); 
    return text;  
}  

我的問題是settextcolor()不起作用。我在網上搜索。據說expandableListView.setCacheColorHint(0);可以解決它。但問題仍然存在。任何人都知道這個問題?

回答

0

您需要訪問特定的XML資源是這樣的:當您運行此

text.setTextColor(getResources().getColor(R.color.expchdhighlightcolor)); 

否則要傳遞的唯一ID引用資源...請注意區別:

Log.v("Test", "Not what I expected: " + R.color.expchdhighlightcolor); 
Log.v("Test", "The hex value in decimal: " + getResources().getColor(R.color.expchdhighlightcolor)); 
+0

山姆, 謝謝。我會嘗試。 – David

+0

它的工作原理!非常感謝。 – David