在 Android XML 中创建此形状(附图)?

Creating this shape (Attached picture) in Android XML?

我知道这个问题很简短,但是如何在 XML 中为 Android Studio 项目创建这个形状?

似乎我可以创建一个矩形,然后从中删除一个半圆形部分,但在 XML 中实现该效果似乎非常困难。有人做过吗?

您好,您可以使用在线转换工具http://www.online-convert.com/ after convert svg to xml android file this site http://inloop.github.io/svg2android/

使用纯 xml 可绘制对象创建这样的背景总是很棘手,但有可能。

我已经在 xml 中创建了您需要的按钮。将此文件添加到您的 drawable 文件夹:

circle.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <!-- inset - moves circle to the left-->
        <inset android:insetLeft="-150dp">
            <shape android:shape="ring"
                    android:thicknessRatio="7"
                    android:innerRadius="0dp"
                    android:useLevel="false"
                    >
                <stroke android:width="2dp" android:color="#000000"/>
                <solid android:color="#ffffff"/>
            </shape>
        </inset>
    </item>
</selector>

custom_button_default.xml

<?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="#00ff00"/>
            <stroke
                    android:color="#000000"
                    android:width="2dp"/>
        </shape>
    </item>
    <item android:drawable="@drawable/circle" />
</layer-list>

custom_button_pressed.xml

<?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="#dddddd"/>
            <stroke
                    android:color="#000000"
                    android:width="2dp"/>
        </shape>
    </item>

    <item android:drawable="@drawable/circle" />

</layer-list>

custom_button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/custom_button_pressed" />
    <item android:drawable="@drawable/custom_button_default"/>
</selector>

custom_button_selector 可绘制对象设置为 layout 中视图的背景:

<Button
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:id="@+id/button"
        android:background="@drawable/custom_button_selector"
        />

您可能需要在 layout 中调整圆圈 insetLeft 值和按钮 widthheight。最好在 values/dimens.xml.

中定义此变量

默认状态:

按下状态:

我已经在 Nexus 5 和 Nexus 7 上测试过 - 按钮背景看起来一样。