【发布时间】:2013-07-24 22:43:50
【问题描述】:
我是 Android 新手。
我正在尝试在 ExpandableListView 适配器中设置自定义颜色。我已经在 colors.xml 中定义了我的颜色,但是我无法在我的适配器中使用它们。我收到一个错误“ExpandableListAdapter 类型的方法 getResources() 未定义”
该函数需要一个 int。我试图从 getResources 中传递我的结果,但是它不起作用。我也尝试过传入一个十六进制值,但它并没有改变任何东西。
如何在我的代码中使用我的自定义颜色?
public View getGroupView(int groupPosition, boolean arg1, View convertView,
ViewGroup arg3) {
int n = 0;
String laptopName = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_item, null);
}
TextView item = (TextView) convertView.findViewById(R.id.demo);
item.setTypeface(null, Typeface.BOLD);
item.setText(laptopName);
convertView.setBackgroundColor(getResources().getColor(R.color.purple));
return convertView;
}
谢谢大家,下面的sn-p可以工作了
this.context = (Activity) context;
convertView.setBackgroundColor(this.context.getResources().getColor(R.color.purple));
【问题讨论】:
标签: android android-layout android-ui