钻石形状xml背景为机器人视图

时间:2017-04-26 06:10:37

标签: android xml android-drawable shape

嗨,有人请建议我如何创建像下面的图像一样的android xml drawable?

enter image description here

现在我使用下面的代码

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <rotate
            android:fromDegrees="45"
            android:pivotX="-40%"
            android:pivotY="87%"
            android:toDegrees="45">
            <shape android:shape="rectangle">
                <stroke
                    android:width="10dp"
                    android:color="@color/colorAccent" />
                <solid android:color="@color/colorAccent" />
            </shape>
        </rotate>
    </item>
</layer-list>

但它的结果是三角形,但我需要以上形状。

4 个答案:

答案 0 :(得分:6)

检查一下,

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:left="43dp"
    android:top="43dp"
    android:right="43dp"
    android:bottom="43dp">
    <rotate android:fromDegrees="45">
        <shape>
            <size
                android:width="200dp"
                android:height="200dp" />
            <gradient
                android:startColor="#0fdcc9"
                android:endColor="#0fdcc9" />
            <stroke
                android:width="5dp"
                android:color="#2DACA0" />
        </shape>
    </rotate>
</item>
</layer-list>

答案 1 :(得分:1)

这是我的菱形ic_diamond.xml文件的可绘制文件。

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="372.7dp"
android:height="367.4dp"
android:viewportWidth="372.7"
android:viewportHeight="367.4">

<path
    android:fillColor="#50AE89"
    android:strokeColor="#266D4F"
    android:strokeWidth="5"
    android:strokeMiterLimit="10"
    android:pathData="M188.706,51.8702 L318.105,181.269 L183.968,315.406 L54.5691,186.007
                   L188.706,51.8702 Z" />
</vector>

我在简单的视图中包含了这个:

<View
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:layout_centerInParent="true"
    android:background="@drawable/ic_diamond"
    />

这是结果:

enter image description here

答案 2 :(得分:0)

我修复了本教程 here

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="43dp"
        android:left="43dp"
        android:right="43dp"
        android:state_pressed="true"
        android:top="43dp">
        <rotate android:fromDegrees="45">
            <shape>
                <size
                    android:width="200dp"
                    android:height="200dp" />
                <gradient
                    android:endColor="@color/colorPrimaryDark"
                    android:startColor="@color/colorPrimaryDark" />

                <stroke
                    android:width="5dp"
                    android:color="@color/colorPrimary" />
                <corners android:radius="7dp" />
                <padding
                    android:bottom="10dp"
                    android:left="10dp"
                    android:right="10dp"
                    android:top="10dp" />
            </shape>
        </rotate>
    </item>
</layer-list>

答案 3 :(得分:-1)

这是一个可绘制的资源,它创建一个200dp的正方形并将其旋转45°以形成菱形:

488
477