如何在android中更改Listview项目颜色和textcolor

时间:2016-03-08 13:15:52

标签: java android android-layout listview android-studio

在我的应用中,我创建了一个Listview。当我点击该行时,我想将Listview行颜色更改为Lightgray颜色,将textcolor更改为蓝色。

为此,我尝试了下面的代码,但只有行背景颜色不会改变文本颜色。

mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                for (int j = 0; j < parent.getChildCount(); j++) {

                    parent.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);
                    view.setBackgroundColor(Color.LTGRAY);

                    rowText = (TextView) findViewById(R.id.rowitem);
                    rowText.setTextColor(Color.BLUE);
                }


   }
    });
}

3 个答案:

答案 0 :(得分:0)

首先创建一个这样的选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/red"
          android:state_pressed="true" />
    <item android:drawable="@android:color/white" />
</selector>

然后

StateListDrawable selector = new StateListDrawable();
selector.addState(new int[] { android.R.attr.state_pressed }, getResources().getDrawable(R.color.red));
selector.addState(new int[] {}, getResources().getDrawable(R.color.white));
view.setBackgroundDrawable(selector);

更改

 rowText = (TextView) findViewById(R.id.rowitem);

rowText = (TextView)view.findViewById(R.id.rowitem);

因为你应该在膨胀的视图中找到textview,而不是独立的。

答案 1 :(得分:0)

首先在ListView中启用android:ListSelector属性

ListView android:id="@+id/android:list" android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_below="@+id/Tablayoutdesign"
    android:cacheColorHint="#000000"
    android:dividerHeight="1dip"
    android:layout_marginTop="63dip"
    android:layout_marginBottom="40dip"

    />

感谢创建一个选择器(listselector.xml)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

  <!-- Selected --> 
  <item 
    android:state_focused="true" 
    android:state_selected="false" 
    android:drawable="@drawable/focused"/> 

  <!-- Pressed -->
  <item 
    android:state_selected="true" 
    android:state_focused="false"
    android:drawable="@drawable/selected" /> 

</selector> 

转到colors.xml

<resources>
<drawable name="focused">#ff5500</drawable>
<drawable name="selected">#FF00FF</drawable>
</resources>

以及一些Java魔术: listview.setSelector(R.drawable.listselector)

感谢@ sankar-anesh

答案 2 :(得分:0)

列出状态按下

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#4285f4" />
            <corners android:radius="2dp" />
        </shape>
    </item>

    <item
        android:bottom="2dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <shape android:shape="rectangle">
            <solid android:color="#4285f4" />
            <corners android:radius="2dp" />
        </shape>
    </item>
</layer-list>

未按下时列出视图背景

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#FFF" />
            <corners android:radius="2dp" />
        </shape>
    </item>

    <item
        android:bottom="2dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <shape android:shape="rectangle">
            <solid android:color="#FFF" />
            <corners android:radius="2dp" />
        </shape>
    </item>
</layer-list>

列表视图选择器

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/card_state_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/card_background" />
</selector>