Android RelativeLayout change color onClick

前端 未结 4 592
猫巷女王i
猫巷女王i 2020-12-09 15:53

How do i change the color of a Relative Layout i use as a clickable on Click like the normal Button? Like i want a visual feedback the layout was pressed.

I tried it

4条回答
  •  执笔经年
    2020-12-09 16:19

    Try the following steps:

    In res --> values folder create color.xml with the content:

    
         
        #000000 
        #ffffff
    
    

    As tag in selector requires a drawable attribute or child tag defining a drawable, your layout_selector.xml file (which is saved in res --> drawable) should look like this:

     
       
         
        
         
     
    

    Also, as said earlier, the relative layout should be clickable (android:clickable="true")

    and its background set as android:background="@drawable/layout_selector"

    Hope it helps

提交回复
热议问题