选择器设置的ListView的TextViews中的文本颜色仅适用于" normal"州

时间:2012-07-12 09:33:09

标签: android

我创建了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)); 

我认为我的错误。

2 个答案:

答案 0 :(得分:2)

text1.setTextColor(getResources().getColorStateList(R.color.dark));

那是我的问题。现在它很棒。

答案 1 :(得分:0)

强制onListItemClik()内的状态。例如:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    v.setSelected(true);

}