ListView item background via custom selector

前端 未结 10 1540
深忆病人
深忆病人 2020-11-22 08:19

Is it possible to apply a custom background to each Listview item via the list selector?

The default selector specifies @android:color/transparent for t

10条回答
  •  野性不改
    2020-11-22 09:13

    The solution by dglmtn doesn't work when you have a 9-patch drawable with padding as background. Strange things happen, I don't even want to talk about it, if you have such a problem, you know them.

    Now, If you want to have a listview with different states and 9-patch drawables (it would work with any drawables and colors, I think) you have to do 2 things:

    1. Set the selector for the items in the list.
    2. Get rid of the default selector for the list.

    What you should do is first set the row_selector.xml:

    
    
        
        
        
        
    
    

    Don't forget the android:state_selected. It works like android:state_focused for the list, but it's applied for the list item.

    Now apply the selector to the items (row.xml):

    
    
    ...
    
    

    Make a transparent selector for the list:

    
    

    This should do the thing.

提交回复
热议问题