MacaronLover MacaronLover - 4 years ago 200
Android Question

How to draw vertical shape in the centre of XML drawable

How can a vertical line be drawn within an XML drawable when using a

layer-list
? I declared 3 shapes within the drawable but for some reason it doesn't appear as expected.

<?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="@color/green"/>
</shape>
</item>

<item android:top="0dp" android:left="1dp" android:bottom="0dp" android:right="5dp">
<shape android:shape="rectangle">
<size android:width="3dp"/>
<solid android:color="@android:color/white"/>
</shape>
</item>
<item android:top="0dp" android:left="5dp" android:bottom="0dp" android:right="1dp">
<shape android:shape="rectangle">
<size android:width="3dp"/>
<solid android:color="@android:color/white"/>
</shape>
</item>
</layer-list>


Current result

enter image description here

Expected result

enter image description here

Answer Source
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:width="16dp"
    android:left="15px">
    <shape
        android:shape="rectangle">
        <solid android:color="@color/colorAccent" />/>
        <size android:width="5dip" />
        <stroke android:color="@color/colorAccent" />
    </shape>
</item>
<item
    android:width="16dp"
    android:left="80px">

    <shape
        android:shape="rectangle">
        <solid android:color="@color/colorAccent" />/>
        <size android:width="5dip" />
        <stroke android:color="@color/colorAccent" />
    </shape>
</item>

<item
    android:width="16dp"
    android:left="140px">
    <shape
        android:shape="rectangle">
        <solid android:color="@color/colorAccent" />/>
        <size android:width="5dip" />
        <stroke android:color="@color/colorAccent" />
    </shape>
</item>

</layer-list>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download