2014-10-01 179 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目錄中使用不同的值。