Android selector with background image and gradient

前端 未结 4 2200
[愿得一人]
[愿得一人] 2020-12-13 08:15

I know there are similar post to this but I couldn\'t find my answer in any of them. So, I have this drawable XML:



        
4条回答
  •  春和景丽
    2020-12-13 08:21

    Your code for the selector is wrong because:

    • You have two elements for the same state and as the selector encounters the first state(state_enabled) for the Bitmap element it will stop there and your gradient will never appear(for this you should use a layer-list that has as items the Bitmap and the gradient on top)

    • The selector will match states in order. As you press the Button the state_pressed will never be activated because the selector will match first the state_enabled that is on the first element(for this you should move the code for the state_pressed above the state_enabled elements).

    In fact you should just remove the state_enabled and let the Bitmap + gradient be the default value for the Button. Bellow is your selector(I assumed you only want to change gradient on the image(but the image should appear even in the pressed state, if this isn't the wanted behavior leave only the gradient for the state_pressed)):

    
    
    
        
            
                
                    
                
                
                    
                         
                         
                         
                         
                    
                
            
        
    
        
            
                
                    
                
                
                    
                        
                        
                        
                        
                    
                
            
        
    
    
    
    

提交回复
热议问题