Add only top and bottom border on LinearLayout

wawanopoulos picture wawanopoulos · Apr 22, 2014 · Viewed 63.2k times · Source

I would like to add only a bottom and a top border on my Linearlayout. I have tried to do this :

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:bottom="1dp"
    android:top="1dp">
    <shape android:shape="rectangle">
        <solid android:color="#FFFFFF" />
        <stroke
            android:width="1dp"
            android:color="#000" />
    </shape>
</item>
</layer-list>

But it add a border around the shape..

Could you help me please ?

Answer

Tony Vu picture Tony Vu · Jan 20, 2015

I think you can create this drawable and use it as background:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item>
        <shape android:shape="rectangle" >
            <solid android:color="#000"/>
        </shape>
    </item>
    <item android:bottom="1dp" android:top="1dp">
        <shape android:shape="rectangle" >
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
</layer-list>

Think of is as drawing a rectangle with border color first and then lay on top of it a rectangle with your background color leaving out 1dp on top and at the bottom.