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

后端 未结 24 2732
盖世英雄少女心
盖世英雄少女心 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:06

    To add a 1dp white border at the bottom only and to have a transparent background you can use the following which is simpler than most answers here.

    For the TextView or other view add:

    android:background="@drawable/borderbottom"
    

    And in the drawable directory add the following XML, called borderbottom.xml

    
    
        
            
                
                
            
        
    
    

    If you want a border at the top, change the android:top="-2dp" to android:bottom="-2dp"

    The colour does not need to be white and the background does not need to be transparent either.

    The solid element may not be required. This will depend on your design (thanks V. Kalyuzhnyu).

    Basically, this XML will create a border using the rectangle shape, but then pushes the top, right and left sides beyond the render area for the shape. This leaves just the bottom border visible.

提交回复
热议问题