Android-使用xml制作箭头形状

Android-使用xml制作箭头形状,android,android-button,android-shape,Android,Android Button,Android Shape,我想为我的形状做一个纽扣,像这样 有没有一种方法可以用xml实现这一点? 像设置一些点一样,在我的例子中,我有5个..您需要的是在项目的可绘制xxx文件夹中创建一个形状xml文件,然后使用此形状作为按钮的背景 这是名为arrow\u shape.xml的形状文件: 然后使用它作为按钮的背景,例如 <Button android:layout_width="wrap_content" android:layout_height="wrap_content" an

我想为我的形状做一个纽扣,像这样

有没有一种方法可以用xml实现这一点?
像设置一些点一样,在我的例子中,我有5个..

您需要的是在项目的
可绘制xxx
文件夹中创建一个
形状xml文件
,然后使用此形状作为按钮的背景

这是名为
arrow\u shape.xml
的形状文件:


然后使用它作为按钮的背景,例如

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/arrow_shape"/>

以下是截图:

有关
图层列表的详细信息,请参见

编辑:

请记住,我对形状的宽度和高度使用了某些值。如果更改这些属性,则可能需要更改
顶部、底部和右侧属性的值。因此,在这种情况下考虑在项目的<代码>值< /代码>目录中使用不同的值。< /P> <代码>
<?xml version="1.0" encoding="UTF-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:width="50dp" android:height="10dp" android:right="6dp">
        <shape>
            <solid android:color="@android:color/holo_green_dark"/>
            <corners
                android:bottomLeftRadius="2dp"
                android:topLeftRadius="2dp"/>
        </shape>
    </item>

    <item android:width="7dp" android:height="7dp"
        android:left="50dp">
        <rotate android:fromDegrees="45"
            android:pivotX="0"
            android:pivotY="0">
            <shape>
                <solid android:color="@android:color/holo_green_dark"/>
            </shape>
        </rotate>
    </item>

</layer-list>

对于,您可以使用定义属性的标准:

比如:

  <com.google.android.material.button.MaterialButton
      app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.Button.Triangle"
      ...      />

以您的风格定义:

  <style name="ShapeAppearanceOverlay.Button.Triangle" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSizeTopLeft">0dp</item>
    <item name="cornerSizeBottomLeft">0dp</item>

    <item name="cornerFamilyTopRight">cut</item>
    <item name="cornerFamilyBottomRight">cut</item>
    <item name="cornerSizeTopRight">50%</item>
    <item name="cornerSizeBottomRight">50%</item>
  </style>

圆的
0dp
0dp
切
切
50%
50%

如果背景是动态布局,它会显示其白色通道]()有没有办法在不改变形状的情况下添加按下状态的颜色?@ReejeshPK有。您可以使用
app:backgroundTint
属性。它与选择器一起工作。它工作,谢谢@加布里埃尔·马里奥蒂
  <style name="ShapeAppearanceOverlay.Button.Triangle" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSizeTopLeft">0dp</item>
    <item name="cornerSizeBottomLeft">0dp</item>

    <item name="cornerFamilyTopRight">cut</item>
    <item name="cornerFamilyBottomRight">cut</item>
    <item name="cornerSizeTopRight">50%</item>
    <item name="cornerSizeBottomRight">50%</item>
  </style>