gpt4 book ai didi

android - 如何在 View 的左上角绘制一个三角形?

转载 作者:太空宇宙 更新时间:2023-11-03 11:46:51 26 4
gpt4 key购买 nike

我创建了这个 drawable:

<?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>

在预览中它看起来像这样:

enter image description here

我已经把它放到这个 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 在预览(和手机)上看起来像这样:

enter image description here

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

谢谢。

编辑:有人建议我使用:

<?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 正方形的一半。这是它的作用: enter image description here

最佳答案

我会使用 <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>

enter image description here

关于android - 如何在 View 的左上角绘制一个三角形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45871971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com