Is there an easy way to add a border to the top and bottom of an Android View?

后端 未结 24 2869
盖世英雄少女心
盖世英雄少女心 2020-11-22 13:36

I have a TextView and I\'d like to add a black border along its top and bottom borders. I tried adding android:drawableTop and android:drawableBottom

24条回答
  •  醉话见心
    2020-11-22 14:02

    The currently accepted answer doesn't work. It creates thin vertical borders on the left and right sides of the view as a result of anti-aliasing.

    This version works perfectly. It also allows you to set the border widths independently, and you can also add borders on the left / right sides if you want. The only drawback is that it does NOT support transparency.

    Create an xml drawable named /res/drawable/top_bottom_borders.xml with the code below and assign it as a TextView's background property.

    
    
        
            
                 
            
        
    
           
            
                  
            
        
    
    

    Tested on Android KitKat through Marshmallow

提交回复
热议问题