2014-01-06 160 views
7

我正尝试在我的android项目中为我的imagebuttons创建一个形状,该图形本质上具有左右两侧的半圆。具有半圆边缘的android按钮

我想我可以只使用一个具有半径的形状XML。但是这只是在我需要整个左侧和右侧成为如下图所示的半个圆的角落。

我对外形当前XML看起来是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" 
    android:color="@color/white"> 

    <corners 
     android:radius="60dip" 
     /> 

    <stroke 
     android:width="0dp" 
     android:color="#000000" /> 


    <solid 
     android:color="@color/white" /> 

</shape> 

,但我想获得这样的效果: enter image description here

回答

1

多高是你的按钮?圆角可绘制的形状应该可以工作,您希望半径为按钮高度的一半。

+0

你可以看到我没有在实际的形状xml中设置高度..我应该吗?因为我将我的布局中的高度设置为40dp,无论我把半径设置为它似乎都没有变化..是他们设置半径为动态的方式吗? – erik

+1

您是在真实设备或模拟器上测试吗?如果布局中的按钮为40dp,则需要角点半径为20dp。尝试在真实的设备和/或模拟器上发布屏幕截图,以了解您的布局xml文件 – Matt

+0

嗯看起来你是对的,而且我必须实际发布到设备才能看到预期的/期望的结果。哇,这是一个时间的腰,谢谢 – erik

4

您必须指定每一个角落半径是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="@color/green" /> 
    <size android:height="30dp" /> 
    <corners 
     android:bottomRightRadius="15dp" 
     android:bottomLeftRadius="15dp" 
     android:topRightRadius="15dp" 
     android:topLeftRadius="15dp" 
     /> 
</shape> 

你不应该指定宽度,以便它可以调节宽度为背景绘制。