Android - make an arrow shape with xml

前端 未结 3 747
不知归路
不知归路 2020-11-30 04:02

I want to make a button for my shape like this one

\"enter

Is there a way to d

相关标签:
3条回答
  • 2020-11-30 04:42
    <?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>
    

    green arrow

    0 讨论(0)
  • 2020-11-30 04:46

    With the Material Components library you can just use the standard MaterialButton defining the shapeAppearanceOverlay attribute:

    Something like:

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

    And in your style define:

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

    0 讨论(0)
  • 2020-11-30 04:57

    What you need is to create a shape xml file in your project's drawable-xxx folder and then use this shape as background for a button.

    Here is the shape file called 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>
    

    Then use it as button's background, for example

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

    Here is the screenshot:

    enter image description here

    More info on Layer-List you can find here.

    EDIT:

    Keep in mind though that I used certain values for the shape's width and height. If you change those you might need to change the values of the top, bottom and right attributes. So, in that case consider using different values in your project's values directory.

    0 讨论(0)
提交回复
热议问题