如何在android中设置ListView选定项alternet文本颜色 [英] How to set ListView selected item alternet text color in android

查看:27
本文介绍了如何在android中设置ListView选定项alternet文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有图像视图和文本视图的自定义列表视图.我希望当用户选择一个项目时,textview 颜色应该改变,所有其他 textview 应该保持默认.

I have a custom listview with a imageview and an textview. I want when user select a item the textview color should change and all the other textview should remain default.

这是我的 xmls

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listViewBell"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:choiceMode="singleChoice"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

</LinearLayout>

ListViewItems.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >

    <TextView
        android:id="@+id/txt_bell_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/rihanna_love_the_way_lie"
        android:textColor="@color/black"
        android:textSize="15sp"
        android:textStyle="bold"
        android:typeface="sans" />

    <ImageView
        android:id="@+id/list_bell_image"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/bronze_bell" />
</RelativeLayout>

推荐答案

在 drawable 中创建以下 button_text.xml

Create following button_text.xml in drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

将文本视图的 textColor 更改为:

Change your text view's textColor to:

  <TextView
        android:id="@+id/txt_bell_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/rihanna_love_the_way_lie"
        android:textColor="@color/button_text" //point to that xml
        android:textSize="15sp"
        android:textStyle="bold"
        android:typeface="sans" />

您可以在此处阅读有关颜色状态列表的信息

You can read about color state list here

这篇关于如何在android中设置ListView选定项alternet文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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