5

I need to create a xml shape drawable that draws a rectangle without the top-line (a "u-form"). What I am able to do is draw a rectangle, like so:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<solid android:color="@color/detailrow_bg_normal" />

<corners
    android:bottomLeftRadius="0dp"
    android:bottomRightRadius="0dp"
    android:radius="1dp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp" />

<padding
    android:bottom="2dip"
    android:left="1.5dip"
    android:right="1.5dip"
    android:top="8dip" />

<stroke
    android:width="1dip"
    android:color="@color/detailtable_border" />

But how - if possible, can I define the same shape without the top (or bottom) line?

Bachi
  • 6,408
  • 4
  • 32
  • 30

3 Answers3

11

You could try using a layer-list. See if something like this would work:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
      <shape android:shape="rectangle">
            <solid android:color="@color/detailtable_border" />
        </shape>
   </item>

   <item android:left="1.5dp" android:right="1.5dp" android:bottom="2dp">
      <shape android:shape="rectangle">
        <solid android:color="@color/detailrow_bg_normal" />
      </shape>
   </item>
</layer-list>

This (should) fill the rectangle with the border color, then overlay it with the default background color, leaving the appropriate amount of the right/left/bottom border color showing.

Femi
  • 64,273
  • 8
  • 118
  • 148
2

try this code and also refer this link:::

I achieved a good solution with this one:

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the line -->
    <item android:top="-1dp" android:right="-1dp" android:left="-1dp">
      <shape>
            <solid android:color="@android:color/transparent" />
            <stroke android:width="1dp" android:color="#ffffff" />
      </shape>
    </item>

</layer-list>

This works well in case you need a transparent color but still an open stroke color (In my case i only needed a bottom line). If you need a background color you can add a solid shape color as in above answer.

Community
  • 1
  • 1
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
0

You might be able to achieve this using two shapes and a LayerList, but I think it is both a better solution and easier to use a NinePatch-drawable.

hanspeide
  • 2,819
  • 4
  • 25
  • 33
  • Thought about that but it would require considerable changes to the already implemented solution, I would really prefer the xml shape way). – Bachi Apr 16 '12 at 17:35