如何在android中设置ListView选择项替代文本颜色

时间:2014-01-30 09:10:47

标签: android android-listview

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

这是我的xmls

ListView.xml

<?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>

5 个答案:

答案 0 :(得分:5)

在drawable

中创建以下button_text.xml
<?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更改为:

  <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" />

您可以阅读有关颜色状态列表here

的信息

答案 1 :(得分:2)

就像其他人所说的那样,只需更改onItemClick方法中的颜色即可。此外:

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);


        setItemNormal();
        View rowView = view;
        setItemSelected(rowView);

    }

public void setItemSelected(View view){
    View rowView = view;
    TextView tv = (TextView)rowView.findViewById(android.R.id.text1);
    tv.setTextColor(Color.BLUE);
}

public void setItemNormal()
{
    for (int i=0; i< mDrawerList.getChildCount(); i++)
    {
        View v = mDrawerList.getChildAt(i);
        //TextView txtview = ((TextView) v.findViewById(R.id.menurow_title));
        TextView txtview = ((TextView)v.findViewById(android.R.id.text1));
        txtview.setTextColor(Color.BLACK);
    }
}

通过这样做,我们将所有项目设置为正常,然后再按每个项目更改它。

答案 2 :(得分:1)

为此,您必须为该特定ImageView设置OnClickListener,然后在Adapter Class中单击ImageView更改TextView的颜色。
或者,

mList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
            TextView tv= view.findViewById(resID); 
            tv.setTextColor(color)
        }
    });

答案 3 :(得分:0)

为listView

实施setOnItemClickListener
listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position,
                long arg3) {
            RelativeLayout relativeLayout = (RelativeLayout) view.getParent();
            TextView textView = (TextView) relativeLayout.getChildAt(0);
            ImageView imaegView = (ImageView) relativeLayout.getChildAt(1);

            textView.setTextColor(Color.RED);
           // Perform whatever action for your textView and imageView


        }

    });

答案 4 :(得分:0)

经过多次尝试在导航视图中设置颜色,我发现了在列表视图中设置文本颜色的最简单方法,也许对某人有帮助

res / color / listview_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- This is used when the navigation item is selected -->
    <item android:state_enabled="true"
        android:state_activated="true" android:color="@color/colorAccent" />
    <!-- This is the default text color -->
    <item
        android:color="@android:color/black" />
</selector>

并在 TextView 中设置为 android:textColor

<TextView
    android:id="@+id/tvItem"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/activity_vertical_extra_margin"
    android:textColor="@color/listview_selector"
    android:textSize="@dimen/text_size_normal"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    tools:text="Row item text" />

问题处于正确的状态,请在导航视图中使用 android:state_activated