如何制作左上圆角和左下圆角的形状?

时间:2010-06-16 18:46:01

标签: android

我想制作一个带有左上角圆角和左下角圆角的形状:

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

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

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

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

但上面的形状没有给我我想要的东西。它给了我一个没有任何圆角的矩形。

有人可以帮忙吗?

感谢。

8 个答案:

答案 0 :(得分:57)

虽然已经回答了这个问题(这是一个导致bottomLeftRadius和bottomRightRadius被反转的错误),但是这个bug已经在android 3.1(api level 12 - 在模拟器上测试)中得到修复。

因此,为确保您的drawable在所有平台上看起来都正确,您应该在您的res / drawable-v12文件夹中放置drawables的“更正”版本(即xml中左下角/右半径实际上是正确的)应用程序。这样,使用Android版本&gt; = 12的所有设备都将使用正确的可绘制文件,而使用旧版本android的设备将使用位于res / drawables文件夹中的“变通方法”可绘制文件。

答案 1 :(得分:48)

它看起来像一个错误http://code.google.com/p/android/issues/detail?id=939

最后我要写这样的东西:

            

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

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

 <corners android:radius="1dp"
  android:bottomRightRadius="2dp" android:bottomLeftRadius="0dp" 
  android:topLeftRadius="2dp" android:topRightRadius="0dp"/> 

我必须为左下角圆角指定android:bottomRightRadius =“2dp”(这里是另一个错误)。

答案 2 :(得分:26)

来自documentation

  

注意:每个角必须(最初)提供大于1的圆角半径,否则没有圆角。如果你想要特定的   角落不圆,一个解决方法是使用android:radius来   设置一个大于1的默认角半径,但然后覆盖每个和   每个角落都有你真正想要的值,提供零(“0dp”)   你不想要圆角的地方。

E.g。你必须设置一个android:radius =“”才能做你想做的事情:

<corners 
    android:radius="2dp"
    android:bottomRightRadius="0dp" 
    android:topRightRadius="0dp"/> 

另一个GOTCHA,如果你做这种事情, eclipse中的预览不正确。您实际上必须启动您的应用才能看到实际结果!

答案 3 :(得分:13)

你也可以使用非常小的数字作为你的半径'。

<corners 
  android:bottomRightRadius="0.1dp" android:bottomLeftRadius="2dp" 
 android:topLeftRadius="2dp" android:topRightRadius="0.1dp" />

答案 4 :(得分:9)

对于其他人,有任何API级别的解决方案,您可以将项目放在彼此的示例之上:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- my firt item with 4 corners radius(8dp)
 -->
    <item>
        <shape>
            <solid
                android:angle="270.0"
                android:color="#3D689A" />

            <corners android:topLeftRadius="8dp" />
        </shape>
    </item>
<!-- my second item is on top right for a fake corner radius(0dp)
 -->
    <item
        android:bottom="30dp"
        android:left="50dp">
        <shape>
            <solid android:color="#5C83AF" />
        </shape>
    </item>
<!-- my third item is on bottom left for a fake corner radius(0dp)
 -->
    <item
        android:right="50dp"
        android:top="30dp">
        <shape>
            <solid android:color="#5C83AF" />
        </shape>
    </item>

</layer-list>

浅色结果显示三个项目:

enter image description here

最终结果:

enter image description here

最好的问候。

答案 5 :(得分:6)

此错误已归档here。 这是Android级别小于12的Android设备的错误。 您必须在drawable-v12文件夹中放置正确版本的布局,该文件夹将用于API级别12或更高级别。 并且相同布局的错误版本(角点切换/反转)将被放入默认的drawable文件夹中,该文件夹将由API级别小于12的设备使用。

例如:我必须在右下方设计一个带圆角的按钮。

在'drawable'文件夹中 - button.xml:我必须将左下角四舍五入。

<shape>
    <corners android:bottomLeftRadius="15dp"/>
</shape>

在'drawable-v12'文件夹中 - button.xml:此处放置了正确版本的布局,用于API级别12或更高级别。

<shape>
    <corners android:bottomLeftRadius="15dp"/>
</shape>

答案 6 :(得分:3)

尝试

{{1}}

答案 7 :(得分:0)

这对我有用

  <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorPrimary" />
    <corners
        android:bottomLeftRadius="0dp"
        android:radius="@dimen/dimen_5dp"
        android:topLeftRadius="0dp" />
</shape>

Achived this