如何更改所选ListView项的颜色 [英] How to change colour of selected ListView Item

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

问题描述

嘿,



我使用最新版本的Android SDK和Eclipse开发Android应用程序。



我使用listview在我的数据库中显示mp3曲目。



目前当用户按下曲目的名称时,翻转颜色为深灰色 - 见下面链接。



http://s30.postimg.org/vj6w4uoch/rollover.jpg



我想将翻转颜色改为橙色。



最简单的方法是什么?



我已经尝试在styles.xml中定义它,但由于我不知道正确的语法而失败了。



我会感激一些帮助和建议.. 。



干杯

Hey,

Im using the latest version of Android SDK and Eclipse to develop an android app.

I am using a listview to display mp3 tracks in my database.

Currently when the user presses on the name of a track the rollover colour is dark grey - see below link.

http://s30.postimg.org/vj6w4uoch/rollover.jpg

I would like to change the rollover colour to orange.

Whats the easiest way to do this?

I have tried defining it in styles.xml but fail miserably as I do not know the proper syntax.

I would be grateful for some help and advice...

Cheers

推荐答案

我的android开发有点生疏,但你只需要do是更改listItem视图的背景src。因此,在view.onclickListener方法中添加一些代码来更改背景src或颜色。例如



My android development is a little rusty, but all you'd need to do is change the background src of the listItem view. So add to the view.onclickListener method some code to change the background src or colour. Eg

View.OnclickListener(View v){

v.setBackgroundColor(0xFF00FF00);

or

v.setBackgroundColor(Color.green);

}





因此,每次点击该项目时,背景颜色将更改为您放置的HEX代码在括号中。如果你想在view.xml中将视图设置为ImageView,你可以将setBackgroundColor属性更改为背景图像



therefore everytime the item is clicked the background colour will change to what ever HEX code you place in the brackets. You can change the setBackgroundColor property to a background image if you wanted if you set the view as an ImageView in your style.xml


感谢您的回复



很遗憾,您的解决方案会根据指定的颜色永久修复背景颜色。



的工作方式是:



- 在res中创建一个名为drawable的文件夹

- 在该文件夹中放置一个名为hover.xml的文件

- 设置onstate,选中和你选择的颜色的其他属性

- 链接到你的activity_main文件中的这个文件夹,即android:list_selector =drawable / hover



和它有效: - )



感谢您的回复
thanks for your reply

regretably your solution permanently fixed the background colour as per the colour specified.

the way it works is:

- create a folder in res called drawable
- place a file called hover.xml inside that folder
- set the onstate, selected and other properties to a colour of your choice
- link to this folder in your activity_main file i.e. android: list_selector ="drawable/hover"

and it works a treat :-)

thanks for your response


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

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