2014-10-01 108 views
13

我想打一个按钮,我的外形像这样的Android的 - 使箭头形状与XML

enter image description here

有没有办法用XML来做到这一点? 像设置一些点,在我来说,我有5 ..

+0

如果背景是动态布局,则显示其白色通道[![enter image description here](http://i.stack.imgur.com/FKtR1.png)](http://i.stack。 imgur.com/FKtR1.png) – 2016-08-01 10:32:13

回答

20

你需要的是建立在项目的drawable-xxx文件夹shape xml file然后用这个形状背景的按钮。

这里是被称为形状文件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> 

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

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

下面是截图:

enter image description here

更多的信息Layer-List你可以找到here

编辑:

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