Android 通过Selector文件改变GridView或ListView选中的效果

综合技术 2018-05-16

在点击GridView或是ListView的选项时常会遇到需要更改选中或是点击的Item的背景颜色,字体颜色的需求,最常用的方法就是通过编写Selector的方法。

  • 按压下去背景变成红色,放开恢复默认灰色
    //默认灰色
    
    //按下去背景变红色
    
    
  • 按压下去字体变成白色,放开恢复默认黑色
    
    

小心了,上面有个坑!用上面的代码并不能达到预期的效果,点击选项时一点改变也没有。原因就在于编写的顺序。我们需要把上面默认的设置放在最下面,也就是如下:


    
    
    //需要把这条默认情况下的设置放在最下面
    

切记!切记!找问题找了半天,还以为是焦点问题什么的呢!

简书

责编内容by:简书 (源链)。感谢您的支持!

您可能感兴趣的

ListView with CheckBoxes and an opener click I would like to implement a click listener for the CheckBoxes of a ListView that uses a custom adapt...
Android ListView GUI Difficulties http://i.stack.imgur.com/4Irb3.png click above link for screen shot. Ok so i'm having so...
How do I implement a delete button in a ListView a... I'm very new to android and I was given a prewritten app that I must improve. One thing I have to do...
ListView in Windows 8 I'm having some trouble storing a list in my app using Windows 8/Xaml/C#. My app contains two pages ...
Refresh listview from the database after a dialog ... I have a list view that is being generated from a SQL database. The user can click on one of the l...