简体   繁体   中英

Custom shape in android using xml

I am trying to draw a custom shape which I can use as a background for my layout. But I am not able to do so. Is it possible to draw a shape as below using xml in android. I am not getting how to cut the semicircle shape from the vertical centres of rectangle.

在此处输入图片说明

Use layer-list to make this custom shape drawable .

/res/drawable/custom_shape.xml:

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

    <!-- Transparent Rectangle -->
    <item>
        <shape android:shape="rectangle">
            <corners
                android:topLeftRadius="8dp"
                android:topRightRadius="8dp"
                android:bottomLeftRadius="4dp"
                android:bottomRightRadius="4dp" />

            <size
                android:width="300dp"
                android:height="100dp" />
            <solid android:color="#FFFFFF" />
        </shape>
    </item>

    <!-- Left Half-Circle -->
    <item
        android:left="-10dp"
        android:right="290dp"
        android:top="40dp"
        android:bottom="40dp">
        <shape android:shape="oval">
            <solid android:color="#000000" />
        </shape>
    </item>

    <!-- Right Half-Circle -->
    <item
        android:left="290dp"
        android:right="-10dp"
        android:top="40dp"
        android:bottom="40dp">
        <shape android:shape="oval">
            <solid android:color="#000000" />
        </shape>
    </item>

    <!-- Middle Dotted Line -->
    <item
        android:left="10dp"
        android:right="10dp">
        <shape android:shape="line">
            <stroke
                android:width="1dp"
                android:dashWidth="10px"
                android:dashGap="10px"
                android:color="#ff00"/>

        </shape>
    </item>

</layer-list>

USE:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_shape"/>

OUTPUT:

在此处输入图片说明

Hope this will help~

try bellow xml. save it in drawable.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:topLeftRadius="15dp"
android:topRightRadius="15dp"
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
/>
<solid
android:color="#ffffff"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>

<stroke
android:width="1dp"
android:color="#000000"
/>
</shape>

In your case it might be worth to create a resizable bitmap ( 9-Patch drawable). Follow this guide.

It is better to use 9-Patch images. If you can create an image like above you can use https://romannurik.github.io/AndroidAssetStudio/nine-patches.html to create 9-patch image(define expandable area).

Try this xml file in drawable :

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

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle">
<solid android:color="@color/primary" />
<corners android:radius="50dp"/>
</shape>

Please refer the below xml, it may help you out

<?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="@android:color/black" />
        </shape>
    </item>
    <item
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp">
        <shape android:shape="rectangle">
            <size
                android:width="200dp"
                android:height="200dp" />
            <solid android:color="@color/colorAccent" />
            <corners android:radius="5dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="line">
            <solid android:color="@android:color/black" />
            <stroke android:width="1dp"
                android:dashWidth="10px"
                android:dashGap="10px"/>
        </shape>
    </item>
    <item
        android:bottom="103dp"
        android:left="-10dp"
        android:right="205dp"
        android:top="103dp">
        <shape android:shape="oval">
            <size
                android:width="3dp"
                android:height="3dp" />
            <solid android:color="@android:color/black" />
            <stroke android:width="1dp" />
        </shape>
    </item>

    <item
        android:bottom="103dp"
        android:left="205dp"
        android:right="-10dp"
        android:top="103dp">
        <shape android:shape="oval">
            <size
                android:width="3dp"
                android:height="3dp" />
            <solid android:color="@android:color/black" />
            <stroke android:width="1dp" />
        </shape>
    </item>
</layer-list>

Try below code

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

        <solid android:color="#FFFFFF" />
        <corners android:radius="3dip" />
        <stroke
            android:width="1dp"
            android:color="#ED2A3C" /> 
    </shape> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM