Android - 用 xml 制作箭头形状
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26143905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - make an arrow shape with xml
提问by Boldijar Paul
I want to make a button for my shape like this one
我想为我的形状制作一个像这样的按钮


Is there a way to do this with xml ? Like setting some points, in my case I have 5..
有没有办法用 xml 做到这一点?就像设置一些点一样,就我而言,我有 5..
回答by Onik
What you need is to create a shape xml filein your project's drawable-xxxfolder and then use this shape as background for a button.
您需要的是shape xml file在项目drawable-xxx文件夹中创建一个,然后将此形状用作按钮的背景。
Here is the shape file called arrow_shape.xml:
这是名为的形状文件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:
这是屏幕截图:


More info on Layer-Listyou can find here.
Layer-List您可以在此处找到更多信息。
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 valuesdirectory.
请记住,我对形状的宽度和高度使用了某些值。如果您更改这些,您可能需要更改top, bottom and right attributes. 因此,在这种情况下,请考虑在项目values目录中使用不同的值。
回答by XimenaB
<?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>


回答by Gabriele Mariotti
With the Material Components libraryyou can just use the standard MaterialButtondefining the shapeAppearanceOverlayattribute:
使用Material Components 库,您可以只使用MaterialButton定义shapeAppearanceOverlay属性的标准:
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">18dp</item>
<item name="cornerSizeBottomRight">18dp</item>
</style>

