Custom Buttons in Android: How to get border/edge/frame when I read the background from xml?

后端 未结 5 1650
萌比男神i
萌比男神i 2021-02-08 03:02

Using Android Shapes in xml I have defined a gradient which I use as the background for a button.

This all works nice, but there\'s no edge surrounding the button. I wou

5条回答
  •  难免孤独
    2021-02-08 03:15

    I had this problem a while ago. While I don't quite remember why I made each decision, the way I solved it was to use a a shape layer-list. This lets you stack one shape on top of another. For example, the following XML creates a shape with a solid black outline 2px wide, with a 'grey to white to grey' gradient across the middle:

    
    
        
            
                
                
                
            
        
    
        
            
                
                
            
        
    
    

    If you want to be able to change that color dynamically at runtime, then things get a lot messier. Again, the details of why I had to do things a certain way are hazy, but I ended up having to create a custom view class which contained a custom ShapeDrawable. I started off looking at the examples from the ApiDemos app which comes with the SDK - it's a very good resource.

    EDIT: Another reason your stroke might not be appearing is that you forgot the android: before the color="...." bit.

提交回复
热议问题