简体   繁体   English

Android ListView选择器可同时更改背景图像和文本颜色

[英]Android ListView selector change background image and text color at the same time

This is my selector xml. 这是我的选择器xml。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:drawable="@drawable/listview_bg" />
<item android:state_focused="true" android:drawable="@drawable/listview" />
 <item android:state_window_focused="false" android:drawable="@drawable/listview_bg" />   
</selector>

I want to change the background image and the color of the text at the same time, how can I modify the xml code? 我想同时更改背景图片和文本颜色,如何修改xml代码?

I have tried to add android:color="#FFFFFF" at each item lines, but it is not working. 我试图在每个项目行中添加android:color="#FFFFFF" ,但是它不起作用。

What you show us is a drawable selector. 您显示给我们的是一个可绘制的选择器。 So it will select the drawable following the state of the view (pressed, focused, ...). 因此,它将根据视图状态(按下,聚焦,...)选择可绘制对象。

You need to use the same pattern but for a color. 您需要使用相同的图案,但要使用颜色。 Create a directory color in your res directory and inside that color directoy a file selectable_color.xml res目录中创建目录color ,并在该color内部直接创建文件selectable_color.xml

In this file, put the following : 在此文件中,放入以下内容:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#FF0000" />
    <item android:color="#00FF00" />
</selector>

So the selectable_color will be red when pressed and blue otherwise. 因此,按下时selectable_color将为红色,否则为蓝色。 Now in your textviews or buttons, use this color as usual android:textColor:@color/selectable_color . 现在,在您的文本视图或按钮中,像往常一样使用此颜色android:textColor:@color/selectable_color Now when the user will press view, its text color will change from blue to red. 现在,当用户按下视图时,其文本颜色将从蓝色变为红色。

However, if pressed is the not the state you looking for, don't hesitate to chnage it, it's the same thing as for the drawable. 但是,如果按下的不是您想要的状态,请毫不犹豫地更改它,这与绘制对象相同。

I have used the following code in my project: 我在项目中使用了以下代码:

ListView listview = (ListView)findViewById(R.id.listView1);

    /*Initialize values with values*/
    ArrayList<String> listItems = new ArrayList<String>();

    ArrayAdapter<String> adapter;
    //adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listItems);

    adapter=new ArrayAdapter<String>(this,R.layout.list_view_item,listItems);

    listItems.add("WebTouch Tab 1");
    listItems.add("WebTouch Tab 2");
    listItems.add("WebTouch Tab 3");
    listItems.add("WebTouch Tab 4");
    listItems.add("WebTouch Tab 5");
    listItems.add("WebTouch Tab 6");
    listItems.add("WebTouch Tab 7");
    listItems.add("WebTouch Tab 8");
    listItems.add("WebTouch Tab 9");
    listItems.add("WebTouch Tab 10");

list_view_item.xml list_view_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_list_view"
android:textColor="#6699FF"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="18sp"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:paddingLeft="5sp"/>

That way you can set the text color and font. 这样,您可以设置文本的颜色和字体。 I have no idea about the background color thing though. 我对背景色一无所知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM