Android - 使用xml制作箭头形状

Bol*_*aul 17 android android-button android-shape material-design material-components-android

我想为这个形状制作一个按钮

在此输入图像描述

有没有办法用xml做到这一点?喜欢设置一些点,在我的情况下,我有5 ..

Oni*_*nik 24

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

这是形状文件,名为arrow_shape.xml:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- Colored rectangle-->
<item>
    <shape android:shape="rectangle">
        <size 
            android:width="100dp"
            android:height="40dp" />
        <solid android:color="#5EB888" />
        <corners android:radius="0dp"/>
    </shape>
</item>

<!-- This rectangle for the top arrow edge -->
<!-- Its color should be the same as the layout's background -->
<item
    android:top="-40dp"
    android:bottom="65dp"
    android:right="-30dp">
    <rotate
        android:fromDegrees="45">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
        </shape>
    </rotate>
</item>

<!-- This rectangle for the lower arrow edge -->
<!-- Its color should be the same as the layout's background -->
<item
    android:top="65dp"
    android:bottom="-40dp"
    android:right="-30dp">
    <rotate
        android:fromDegrees="-45">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
        </shape>
    </rotate>
</item>

</layer-list>
Run Code Online (Sandbox Code Playgroud)

然后将其用作按钮的背景

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/arrow_shape"/>
Run Code Online (Sandbox Code Playgroud)

这是截图:

在此输入图像描述

Layer-List您可以在此处找到更多信息.

编辑:

请记住,我使用了某些值来表示形状的宽度和高度.如果你改变那些你可能需要改变的值top, bottom and right attributes.因此,在这种情况下,请考虑在项目values目录中使用不同的值.


小智 10

<?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>
Run Code Online (Sandbox Code Playgroud)

绿色箭头


Gab*_*tti 7

使用Material Components 库,您可以只使用MaterialButton定义shapeAppearanceOverlay属性的标准:

就像是:

  <com.google.android.material.button.MaterialButton
      app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.Button.Triangle"
      ...      />
Run Code Online (Sandbox Code Playgroud)

并以您的风格定义:

  <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>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明