Android - 用 xml 制作箭头形状 [英] Android - make an arrow shape with xml

查看:42
本文介绍了Android - 用 xml 制作箭头形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

有没有办法用 xml 做到这一点?就像设置一些点一样,就我而言,我有 5..

Is there a way to do this with xml ? Like setting some points, in my case I have 5..

推荐答案

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

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.

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

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

截图如下:

有关 Layer-List 的更多信息,您可以找到 此处.

请记住,我对形状的宽度和高度使用了某些值.如果您更改这些,您可能需要更改 top、bottom 和 right 属性 的值.因此,在这种情况下,请考虑在项目的 values 目录中使用不同的值.

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.

这篇关于Android - 用 xml 制作箭头形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆