更改ListView的文本颜色上点击 [英] Change ListView's text color on click

查看:76
本文介绍了更改ListView的文本颜色上点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的的ListView 的列表中的每一行是由一个由的ImageView 和两个的TextView 的。我想文字的颜色改为白色列表的项目被点击时(仅在点击项)。另外,我要的颜色改回为黑色时,该项目是unclicked(当preSS被释放)。我已经在该项目的背景颜色的变化,当它被点击以下面的 list_item_bg.xml

I have a customized ListView that each row of the list is composed by an ImageView and two TextView's. I want to change the text's color to white when a list's item is clicked (only in the clicked item). Also, I want to change back the color to black when the item is "unclicked" (when the "press" is released). I already made the item's background color change when it was clicked with the following list_item_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item 
    android:state_selected="false"
    android:state_pressed="false" 
    android:drawable="@color/white" />

<item android:state_pressed="true" 
    android:drawable="@color/red_destaques" />

<item android:state_selected="true"
    android:drawable="@color/red_destaques" />
</selector>

listitem_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="5dip"
    android:maxHeight="50dip"
    android:adjustViewBounds="true"
    android:background="@color/list_item_bg">

    <ImageView
        android:layout_width="70dip"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:adjustViewBounds="true"
        android:scaleType="fitStart" 
        android:src="@drawable/imagem_padrao_lista" 
        android:id="@+id/listItemLogo">
    </ImageView>

    <TextView
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/listItemLogo"
        android:layout_toLeftOf="@+id/listItemArrow"
        android:textColor="#000000"
        android:layout_marginLeft="5dip"
        android:adjustViewBounds="true" 
        android:id="@+id/listItemTitle">
    </TextView>

    <TextView
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/listItemLogo"
        android:layout_below="@+id/listItemTitle"
        android:textColor="#333333"
        android:layout_marginLeft="5dip"
        android:adjustViewBounds="true" 
        android:id="@+id/listItemDescription">
    </TextView>
    <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:src="@drawable/setinha_navegacao" android:layout_centerVertical="true" android:id="@+id/listItemArrow"></ImageView>

</RelativeLayout>

余的文本改变它的颜色完全以相同的方式,作为示出在上述XMLS背景的变化。我想preFER这样的变化由code如果可能的话...

I the text to change it's color exactly in the same manner that the background changes as shown in the above xmls. I would prefer doing this change by code if possible...

推荐答案

创建一个新的StateListDrawable像以前那样,但采用黑色作为默认的颜色和白当pressed。

Create a new StateListDrawable like you did before but with black as default color and white when pressed.

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

现在在TextView中更改文本颜色为新绘制:

Now for in the TextView change the text color to the new drawable:

android:textColor="@color/list_item_text"

更多关于StateListDrawables:<一href="http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList">http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

这篇关于更改ListView的文本颜色上点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆