我是Android新手。無法在ExpandableListView中設置自定義顏色
我想在ExpandableListView適配器內設置自定義顏色。我在colors.xml中定義了我的顏色,但我無法在我的適配器中使用它們。我得到一個錯誤「方法getResources()未定義類型ExpandableListAdapter」
函數需要一個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;
}
謝謝你們,下面的代碼片段工作
this.context = (Activity) context;
convertView.setBackgroundColor(this.context.getResources().getColor(R.color.purple));
謝謝。我會發布我的更新代碼 – user1801060