android - Add a border to the top and bottom of a View

activity_main.xml
 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/rl"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dp"     tools:context=".MainActivity"     android:background="#cad2ca"     >     <TextView         android:id="@+id/tv"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerInParent="true"         android:textSize="25sp"         android:gravity="center"         android:textColor="#000000"         android:textStyle="bold"         android:text="TextView Top Bottom Border"         android:background="@drawable/view_top_bottom_border"         android:padding="25dp"         /> </RelativeLayout> 
res/drawable/view_top_bottom_border.xml
 <?xml version="1.0" encoding="utf-8"?> <!--     layer-list         A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable         in the list is drawn in the order of the list—the last drawable in the list is drawn on top.          Each drawable is represented by an <item> element inside a single <layer-list> element. --> <layer-list     xmlns:android="http://schemas.android.com/apk/res/android"     >     <!--         To change the background color of the view, change the both solid color.         To change border color and width, change the both stroke color and both stroke width.     -->     <item>         <shape android:shape="rectangle">             <solid android:color="#ffffff"/>             <stroke android:color="#ff0008" android:width="2dp"/>         </shape>     </item>     <!--         item             Defines a drawable to place in the layer drawable, in a position defined             by its attributes.              android:top                 Integer. The top offset in pixels.             android:right                 Integer. The right offset in pixels.             android:bottom                 Integer. The bottom offset in pixels.             android:left                 Integer. The left offset in pixels.     -->     <item android:bottom="2dp" android:top="2dp">         <shape android:shape="rectangle">             <solid android:color="#ffffff"/>             <!--stroke android:color="#246921" android:width="2dp"/-->         </shape>     </item> </layer-list> 

Komentar