2010-02-22 41 views
8

我无法使用XML创建简单的圆角矩形。每次我尝试了“弯道”元素添加到自定义形状,我得到:在 android.graphics.Path.addRoundRect(Path.java:514) Android - 无法创建简单的矩形形状... UnsupportedOperationException?

java.lang.UnsupportedOperationException 在 机器人.graphics.drawable.GradientDrawable.draw(GradientDrawable.java:314) 在 android.view.View.draw(View.java:6520) ...

RES/dawable/rounded_rectangle.xml:

使用上述形状
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
     <solid android:color="#ffffff"/>  

     <stroke android:width="3dp" 
       android:color="#ff000000"/> 

     <padding android:left="1dp" 
       android:top="1dp" 
       android:right="1dp" 
       android:bottom="1dp"/> 

     <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 
    </shape> 

简单layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"> 

<View android:id="@+id/View01" 
    android:background="@drawable/rounded_rectangle" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
</View> 
</RelativeLayout> 

据透露,我试图编译Android 2.1的,我都安装到Eclipse的最新动态和Android SDK。这种形状是我在另一个网站上看到的东西的直接副本,但由于某种原因,它不想为我工作。

谢谢。

+0

请参阅Shape元素及其属性: http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape – vanna

回答

34

所以,我只是玩了一下,我在round_rectangle.xml中改了几行来让它工作。见下:

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

    <stroke android:width="3dp" 
      android:color="#ff000000"/> 

    <padding android:left="1dp" 
      android:top="1dp" 
      android:right="1dp" 
      android:bottom="1dp"/> 

    <corners android:radius="30dp"/> 
</shape> 

我只希望谷歌能推出一个适当的参考文档来创建基于XML的形状。在Web上搜索示例数小时(4+)之后,我觉得这仍然是猜测这些XML文档中支持哪些元素/属性的游戏。对不起,迷你咆哮。

我希望这可以帮助别人。

+2

小挑剔,但使用像素(px)通常是一个糟糕的主意。应该可能是<角落android:radius =“30dp”/> – DougW

+2

它工作的原因是因为您将4个单独的半径值合并为1。请参阅此错误http://code.google.com/p/android/issues/细节?ID = 7588 –