38

I want to set the background of a Relative or LinearLayout to a custom drawable. I want the shape to draw two horizontal lines across the bottom, leaving the central part transparent (empty).

The following draws the horizontal lines centered vertically, where as I need them to be aligned to the bottom of the shape. (If you add a rectangle as an item you can see the shape expands to the dimenstions of the parent, but the lines are still centrally aligned).

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
 <item>
  <shape android:shape="line">
      <stroke android:width="1dip" android:color="#99b8b9bd" />
      <size android:height="1dip" />
  </shape>
 </item>
 <item>
  <shape android:shape="line" android:top="1dip">
      <stroke android:width="1dip" android:color="#FFFFFFFF" />
      <size android:height="1dip" />
  </shape>
 </item>
</layer-list>
peterh
  • 11,875
  • 18
  • 85
  • 108
Emile
  • 11,451
  • 5
  • 50
  • 63

2 Answers2

62

Found the answer before i'd finished asking about it.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="@color/hr_bottom" />
            <solid android:color="#00FF0000" />
            <padding android:bottom="1dp"/>
        </shape>
   </item>

   <item>
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="@color/hr_top" />
            <solid android:color="#00FF0000" />
            <padding android:bottom="1dp"/>
        </shape>
   </item>

    <item>
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="#00000000" />
            <solid android:color="#00000000" />
        </shape>
   </item>

</layer-list>
Emile
  • 11,451
  • 5
  • 50
  • 63
  • This sort of similar question had an answere that helped me with the above. http://stackoverflow.com/questions/2224644/drawing-top-border-on-a-shape-on-android – Emile Nov 10 '10 at 16:49
  • 1
    Could you please give me the color values of `hr_bottom` and `hr_top`? I tried this and it drew a rectangle. –  May 12 '12 at 08:59
  • hr_top & hr_bottom are just colours, try = #FF0000 , you'll get a black box with red at the top and bottom – Blundell Aug 08 '13 at 13:33
  • @Emile I've tried this layer-list and got rectangles... Perhaps something changed in the SDK in some version? (I'm trying on KitKat) – Jose_GD Mar 05 '14 at 21:23
  • Very possible. Afraid i don't do much android at the moment, so i can't test it out. – Emile Mar 06 '14 at 10:53
  • it seems very strange to me, that is is necessary to create multiple rectangles covering each other in order to achieve horizontal lines that are not centered. Is it really imploissble to do with lines? – IARI Dec 28 '16 at 10:16
  • Well, this was a solution 7 years ago, so there may be an alternative solution now. As it was then, there were not many alternative methods. – Emile Jan 03 '17 at 10:45
5

You can draw with below simple code also.

<item
    android:right="-2dp"
    android:bottom="-2dp"
    >
    <shape
        android:shape="rectangle">

        <stroke
            android:width="0.5dp"
            android:color="#ff0000"
            android:dashGap="4dp"
            android:dashWidth="4dp" />

    </shape>
</item>

Huy Tower
  • 7,769
  • 16
  • 61
  • 86