我创建了 ListView 和我自己的 ListAdapter。我使用 simple_list_item_2。我想更改每个状态的文本颜色。下面有dark.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/selectedTextListColor"/>
<item android:state_focused="true" android:color="@color/selectedTextListColor"/>
<item android:state_pressed="true" android:color="@color/selectedTextListColor"/>
<item android:color="@color/textListColor"/>
</selector>
接下来,当我尝试设置该功能时,它是代码的一部分。我认为有问题。
ArrayAdapter<Element> adapter = new ArrayAdapter<Element>(instance, android.R.layout.simple_list_item_2, songsArray){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TwoLineListItem row;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = (TwoLineListItem)inflater.inflate(android.R.layout.simple_list_item_2, null);
} else {
row = (TwoLineListItem)convertView;
}
Element song = songsArray[position];
String data = song.text();
int dashIndex = data.indexOf("-");
TextView text2 = row.getText2();
text2.setText(data.substring(0, dashIndex-1).trim());
text2.setTextSize(12.0f);
text2.setPadding(10, text2.getPaddingTop(), text2.getPaddingRight(), text2.getPaddingBottom());
TextView text1 = row.getText1();
text1.setText(data.substring(dashIndex+1).trim());
text1.setTextSize(18.0f);
text1.setPadding(10, text1.getPaddingTop(), text1.getPaddingRight(), text1.getPaddingBottom());
text1.setTextColor(getResources().getColor(R.color.dark));
return row;
}
};
它仍然以这种方式工作,文本颜色为红色,并且在印刷时没有改变。你能告诉正确的方法吗?线上注意
text1.setTextColor(getResources().getColor(R.color.dark));
我认为这是我的错误。