Java Android - 无法创建简单的矩形形状... UnsupportedOperationException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2310209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:58:59  来源:igfitidea点击:

Android - Can't create simple rectangle shape... UnsupportedOperationException?

javaandroid

提问by RyanM

I'm having trouble creating a simple rounded rectangle using XML. Every time I try to add the "corners" element to the custom shape I get:

我在使用 XML 创建简单的圆角矩形时遇到问题。每次我尝试将“角”元素添加到自定义形状时,我都会得到:

java.lang.UnsupportedOperationException at android.graphics.Path.addRoundRect(Path.java:514) at android.graphics.drawable.GradientDrawable.draw(GradientDrawable.java:314) at android.view.View.draw(View.java:6520) ...

java.lang.UnsupportedOperationException at android.graphics.Path.addRoundRect(Path.java:514) at android.graphics.drawable.GradientDrawable.draw(GradientDrawable.java:314) at android.view.View.draw(View.java:第6520章 ……

res/dawable/rounded_rectangle.xml:

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>

simple layout.xml using the above 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>

Fyi, I'm trying to compile for Android 2.1 and I have all the latest updates installed to Eclipse and the Android SDK. This shape is a direct copy of something I saw on another website, but for some reason It doesn't want to work for me.

仅供参考,我正在尝试针对 Android 2.1 进行编译,并且我已将所有最新更新安装到 Eclipse 和 Android SDK。这个形状是我在另一个网站上看到的东西的直接副本,但由于某种原因它不想为我工作。

Thanks.

谢谢。

采纳答案by RyanM

So, I was just playing around with this a bit and I changed a couple of lines in the rounded_rectangle.xml to get it working. See below:

所以,我只是在玩这个,我在 rounded_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>

I only wish Google would put out a proper reference doc for creating XML-based shapes. After hours (4+) of hunting down examples on the Web, I feel like it's still a guessing game as to what elements/attributes are supported in these types of XML documents. Sorry for the mini-rant.

我只希望谷歌能够发布一个适当的参考文档来创建基于 XML 的形状。经过数小时 (4+) 小时在 Web 上寻找示例后,我觉得对于这些类型的 XML 文档中支持哪些元素/属性仍然是一个猜谜游戏。对不起,小咆哮。

I hope this helps someone else.

我希望这对其他人有帮助。