Search code examples
androidcolorsbackgroundshapesandroid-drawable

How to set a shape's background in xml?


I just created a red circle using android shapes:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadiusRatio="4"
    android:shape="ring"
    android:thicknessRatio="9"
    android:useLevel="false" >

     <solid android:color="#FF0000" />

    <size
        android:height="48dip"
        android:width="48dip" />

</shape>

This is really cool, but I cannot set the background color of the circle to my color. I tried android:background="#FFFFFF" but it always appear to be black in my layout. How can I set the background of the above shape?


Solution

  • I think a layer-list might help you:

    <?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="#ffffff" />
            </shape>
        </item>
        <item>
            <shape
                android:innerRadiusRatio="4"
                android:shape="ring"
                android:thicknessRatio="9"
                android:useLevel="false" >
                <solid android:color="#FF0000" />
                <size
                    android:height="48dip"
                    android:width="48dip" />
            </shape>
        </item>
    
    </layer-list>