简体   繁体   English

在android中更改listview行项目背景颜色和文本颜色

[英]Change listview row item background color and text color in android

In my custom listview, I have used relativelayout and inside that added 2 textviews vertically. 在我的自定义列表视图中,我使用了relativelayout和inside,在垂直方向上添加了2个文本视图。 Background image is set to relativelayout. 背景图像设置为relativelayout。 Now I want to change background image as well as textcolor of textview when listview item is pressed. 现在,我想在按下listview项目时更改背景图像以及textview的textcolor。 How to do this, any one have idea? 怎么做,任何人都有想法?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/item_bg"
    android:orientation="vertical" >
    <TextView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text=""
    android:textColor="@color/orange"
    android:textSize="18sp"
    android:textStyle="bold"
    android:layout_marginTop="10dip"
    android:layout_marginLeft="10dip"
    />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="20dip"
    android:layout_marginTop="8dip"
    android:ellipsize="end"
    android:maxLines="3"
    android:duplicateParentState="true"   <!-- this is a line to use..-->
    android:textColor="@color/_textcolor"
    android:textSize="14sp" >

</TextView>

item_bg.xml inside res/drawable folder: res / drawable文件夹中的item_bg.xml:

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

_textcolor.xml inside res/color/ folder: res / color /文件夹中的_textcolor.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="@color/white"/>
<item android:state_focused="true"  android:color="@color/white" />
<item android:state_selected="true" android:color="@color/white"/>
<item android:color="@color/dark_blue"/>
</selector>

Concerning the background, you should create a StateList Drawable and set it as the background of your rows. 关于背景,您应该创建一个StateList Drawable并将其设置为行的背景。

Ex of a such drawable (could be named background_selector.xml) : 这样的drawable(可以命名为background_selector.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/color1" /> <!-- pressed -->
    <item android:drawable="@color/color2" /> <!-- default -->
</selector>

And in the layout declaration of your row : 并在您的行的布局声明中:

...
android:background="@drawable/background_selector"
...

Use the same way for your text with the Color State List 使用颜色状态列表对文本使用相同的方法

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

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