Android - How can I create a selector for Layouts (like an ImageButton selector)

前端 未结 2 1056
清歌不尽
清歌不尽 2021-01-15 03:28

I have an ImageButton and a LinearLayout wrapping that button, like this:



        
2条回答
  •  天涯浪人
    2021-01-15 04:28

    I think the questioner is dead by now :XD

    but this is the solution

    add this two lines of code to your layout:

    android:clickable = "true"
    

    and

    android:focusable = "true"
    

    the main sample is

    we have this constraint layout like :

    
    
    

    and the ( layout_selector.xml ) like this :

    
    
         
         
         
    
    

    and for ( bg_pressed.xml ) and ( bg_focused.xml ) and ( bg_default.xml ) as we used in selector before we need to declare them in drawable folder like :

    bg_pressed.xml :

    
    
    
        
        
    
        
    
    

    bg_focused.xml :

    
    
    
        
        
    
        
    
    

    bg_default.xml :

    
    
    
        
        
    
    

    now put your image view or something else inside the layout and try some clicks :) (read the question!)

    hope this will help you ...

提交回复
热议问题