Search code examples
androidandroid-drawablelayer-list

How to create a xml drawable like this


I need to create a drawable with a 3dp height line/rectangle on top, a black rectangle with 70% opacity and 2dp height line/rectangle on the bottom. How can I do this?

This is what I could do until now

This is what happens if I set the color to #99000000

Here is the code I'm using:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="0dp" android:left="0dp" android:right="0dp">
        <shape android:shape="rectangle" >
            <size android:height="3dp"></size>
            <solid android:color="#C81F25"></solid>
        </shape>
    </item>
    <item android:top="2dp">
        <shape android:shape="rectangle">
            <solid android:color="#99000000"></solid>
        </shape>
    </item>

</layer-list>

PS: Ignore the gray gradient around the image, it's from Android Studio preview.


Solution

  • Case 1: Rectangle inside another rectangle.

    Case 2: 3 Rectangles of which 2 are transparent which have negative paddings and strokes. On eclipse previews the case 2 shows wrong but it is correct and works when deployed.

    case 1: actionbarbg.xml in your drawable folder:

    <?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="#F00" />
            </shape>
        </item>
        <item android:top="3dp" android:right="0dp" android:bottom="2dp"
            android:left="0dp">
            <shape android:shape="rectangle">
                <solid android:color="#9000" />
            </shape>
        </item>
    </layer-list>
    

    case 2: (sort of a hack)

    <?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="#9000" />
            </shape>
        </item>
        <item
            android:top="-2dp"
            android:left="-2dp"
            android:right="-2dp">
            <shape>
                <solid android:color="@android:color/transparent" />
    
                <stroke
                    android:width="2dp"
                    android:color="#C81F25" />
            </shape>
        </item>
        <item
            android:left="-3dp"
            android:right="-3dp"
            android:bottom="-3dp">
            <shape>
                <solid android:color="@android:color/transparent" />
    
                <stroke
                    android:width="3dp"
                    android:color="#C81F25" />
            </shape>
        </item>
    
    </layer-list>