1

我在活动中有一个 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); 可以解决。但问题仍然存在。有人知道这个问题吗?

4

1 回答 1

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));
于 2012-05-29T04:28:27.980 回答