简体   繁体   English

更改所选ListView项的颜色

[英]Change color of an selected ListView item

i use a default android project and try to find a way to change the background color of a listview item that got selected. 我使用默认的android项目,并尝试找到一种方法来更改已选中的listview项目的背景颜色。 I know this is done via selectors. 我知道这是通过选择器完成的。 I tried to mimic the original look and behaviour coming from the holo themes and just change the color in there, but i failed. 我试图模仿来自整体主题的原始外观和行为,只是更改其中的颜色,但是我失败了。 Can somebody explain to me how to get the original look and feel for listview items (longclick fade etc) and just change the color from this strange blue to something else? 有人可以向我解释如何获得列表视图项目的原始外观和感觉(longclick淡入淡出等),然后将颜色从这种奇怪的蓝色更改为其他颜色吗?

What do I have to copy to my project from android sdk and where to change the selected color? 我必须从android sdk复制什么到我的项目,以及在哪里更改所选颜色?

Assuming you're using a custom ArrayAdapter , you'll have to define your onClick() listener on the second parameter of your getView() method. 假设您使用的是自定义ArrayAdapter ,则必须在getView()方法的第二个参数上定义onClick()侦听器。 Usually it's called convertView , but it doesn't have to. 通常将其称为convertView ,但并非必须如此。

This second parameter, speaking very vaguely, references to the layout of each row. 说得很模糊,第二个参数引用了每一行的布局。 So when you want to change the background of your row, simply add a onClickListener() on the second parameter, and change the background: 因此,当您想更改行的背景时,只需在第二个参数上添加onClickListener() ,然后更改背景:

convertView.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    convertView.setBackground(your_new_background);
  }
}); 

dude you can use custom adapter for your listview. 老兄,您可以为列表视图使用自定义适配器。 In custom xml files u can change everthing. 在自定义xml文件中,您可以进行任何更改。 For detail example follow here 有关详细示例,请点击此处

I am not sure what you asking , but watever I understood I think you want something like 我不确定您要问的是什么,但我知道我想知道您想要什么

listview.setOnItemClickListener(new OnItemClickListener(){

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

       ..............................
            view.setBackground("Your color");
  }
});

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

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