Change background color of an item in Android ListActivity onListItemClick

后端 未结 6 1037
醉酒成梦
醉酒成梦 2021-02-14 09:12

I know it sounds very simple, and there are questions about this. But none of it could solve my problem. So here we go:

I want to change background color of a list item

6条回答
  •  隐瞒了意图╮
    2021-02-14 09:34

    What I do is I create an xml file called i.e. list_background and put it in the drawable folder.

    The xml looks like this:

    
       
       
    
    

    And in the xml code for the ListView's item I put this xml as the items background i.e.

    item.xml:

    
    
         
    
    

    style=@style/Fill is only a short cut i made for android:layout_height="match_parent" and android:layout_width="match_parent

    Then in onListItemCLick:

    public void onListItemClick(ListView l, View v, int position, long id) {
        v.setPressed( !v.isPressed ) //Toggle between colors of the view
    }
    

提交回复
热议问题