18

在我的 android xml 布局中,我使用borderframe.xml 作为背景来应用边框。

borderframe.xml 文件如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke 
        android:width="1dip" 
        android:color="#ffffff"/>
    <solid 
        android:color="#95865F"/>
    <corners 
        android:radius="10px"/>

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

现在,虽然有一个 android:radius="10px" 那么它是有效的,但是虽然我要给特定的角落赋予圆形,但它不起作用。log cat 中没有任何错误消息,但我在 eclipse 中发现错误,例如:

    The graphics preview in the layout editor may not be accurate:
* Different corner sizes are not supported in Path.addRoundRect.

即使该 xml 文件中没有填充,我也看不到任何边框。

现在,我该怎么做呢?如果我只想为 topLeftcorner 和 bottomLeftCorner 创建圆形边框,它的解决方案是什么。? 谢谢。

4

4 回答 4

17

我也面临同样的问题。但为此我使用图层列表。我在这里发布我的答案,这可能会对您有所帮助。
请检查输出屏幕图 1

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="#c1c1c1" />
            <solid android:color="#c1c1c1" />
            <corners android:radius="20dp"/>
        </shape>
   </item>

   <item android:right="20dp"
        >
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="#c1c1c1" />
            <solid android:color="#c1c1c1" />
        </shape>
   </item>

</layer-list>
于 2014-04-01T06:57:00.480 回答
10

你必须这样做,假设你只想要一个圆形的左上角:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
  <corners
      android:radius="20sp"
      android:topRightRadius="0dp"
      android:bottomRightRadius="0dp"
      android:bottomLeftRadius="0dp" />
  <gradient
      android:startColor="@color/logo_blue"
      android:endColor="@color/blue"
      android:angle="0"/>
</shape>

解释:必须(最初)为每个角提供大于 1 的角半径,否则不会圆角。如果您希望特定角不被圆角,一种解决方法是使用android:radius设置大于 1 的默认角半径,然后用您真正想要的值覆盖每个角,在您的位置提供零(“0dp”)不想要圆角。[来源]

因此,您需要将您的可绘制对象定义为:

<?xml version="1.0" encoding="UTF-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke 
        android:width="1dip" 
        android:color="#ffffff"/>
    <solid 
        android:color="#95865F"/>
    <corners 
       android:radius="10px"
      android:topRightRadius="0dp"
      android:bottomRightRadius="0dp" />

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

更新

来自Shape Drawable Resource Android 文档

机器人:半径尺寸。所有角的半径,作为尺寸值或尺寸资源。这被以下属性覆盖每个角。

覆盖是您问题的关键字...</p>

于 2011-12-06T12:00:42.610 回答
6

我正在使用 SDK 工具 19、平台工具 11 并在 Android 4.0.3 中运行该应用程序。使用以下 XML 对我有用:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
    <solid android:color="#dd000000"/>
    <padding 
        android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners
        android:bottomLeftRadius="25dp"
        android:bottomRightRadius="25dp"
        android:topLeftRadius="0dp"
        android:topRightRadius="0dp" />
</shape>

Eclipse 警告是关于无法显示预览的。在 het 应用程序中,它显示正确。

于 2012-06-14T15:38:07.443 回答
-1

我在其中一个论坛上得到了解决方案。我通过评论每个角落并添加到可绘制的xml来解决它。我保留了下面的注释代码只是为了理解。

XML 代码-

   <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
        <solid android:color="#fd2149" />
        <stroke android:width="1dip" android:color="#ffd102"/>
      <!--  <corners
            android:bottomLeftRadius="8dp"
            android:bottomRightRadius="8dp"
            android:topLeftRadius="8dp"
            android:topRightRadius="8dp" />
        -->
        <corners android:radius="8dp" />
    </shape>

---已编辑- 上面的代码是我在可绘制文件夹中的 back.xml,如果需要,您可以将填充添加到相同的 back.xml。对于新开发人员,back.xml 是从您的布局文件中引用的,如下所示

  <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textSize="20sp"
    android:scrollbars="vertical"
    android:background="@drawable/back">

希望这可以帮助。

于 2016-05-28T12:17:41.740 回答