android triangle drawablw xml

2019-02-27 02:53发布

问题:

I want to draw an equilateral triangle.I have checked but it is inverted.I want a triangle that looks like the image below.

Triangle:

triangle_shape.xml:

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

Currently it looks like this..

回答1:

Usign Vector drawable you can achieved your shape like below:

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="100dp"
    android:height="100dp"
    android:viewportHeight="100"
    android:viewportWidth="100">
    <group android:name="triableGroup">
        <path
            android:name="triangle"
            android:fillColor="@color/color_start_color_back"
            android:pathData="m 50,0 l 50,50 -100,0 z" />
    </group>
</vector>

Output:

I hope its helps you.



回答2:

Try this way it will work

  <?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="#00000000" />

            <solid android:color="#00ACED" />
        </shape>
    </rotate>
</item>
</layer-list>

OUTPUT



回答3:

I have found out the way to make it look like an up facing triangle.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <rotate
            android:fromDegrees="-45"
            android:toDegrees="45"
            android:pivotX="270%"
            android:pivotY="70%" >
            <shape
                android:shape="rectangle" >
                <stroke android:color="#000000" android:width="1dp"/>
                <solid
                    android:color="#000000" />
            </shape>
        </rotate>
    </item>
</layer-list>