如何在视图的左上角绘制一个三角形?

How can I make a triangle drawable on the top left of my view?

我创建了这个可绘制对象:

<?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="10"
            android:pivotX="100%"
            android:pivotY="-0%" >
            <shape
                android:shape="rectangle"   >
                <solid
                    android:color="@color/color_primary"  />
            </shape>

        </rotate>
    </item>
</layer-list>

在预览中看起来像这样:

我已经把它放进去 View:

        <View
        android:id="@+id/my_view"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:background="@drawable/triangle"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

这个 View 在预览(和 phone 上)看起来像这样:

为什么 View 不像三角形预览那样只显示角上的三角形?另外,我应该提到我希望它填满 View 正方形的一半,基本上是从右上角到左下角。

谢谢。

编辑:有人建议我使用:

<?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="-135"
            android:pivotX="90%"
            android:pivotY="-45%" >
            <shape
                android:shape="rectangle"   >
                <solid
                    android:color="@color/color_primary"  />
            </shape>

        </rotate>
    </item>
</layer-list>

这确实会产生一个角三角形,但不会填满 View 正方形的一半。这是它的作用:

将您的 XML 更改为以下内容,它将按照您的意愿工作。

triangle.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="-135"
            android:pivotX="90%"
            android:pivotY="-45%" >
            <shape
                android:shape="rectangle"   >
                <solid
                    android:color="@color/color_primary"  />
            </shape>

        </rotate>
    </item>
</layer-list>

Preview
Device screenshot:

我会使用 <vector> 可绘制对象而不是 <layer-list> 来解决这个问题。

<vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">

    <path
        android:fillColor="@color/color_primary"
        android:pathData="M0 24v-24h24z"/>

</vector>