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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3056232/
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-20 08:30:31  来源:igfitidea点击:

How to make a shape with left-top round rounded corner and left-bottom rounded corner?

androidandroid-shape

提问by user256239

I want to make a shape with with left-top rounded corner and left-bottom rounded corner:

我想用左上圆角和左下圆角制作一个形状:

<?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>

But the shape above didn't give me what I want. It gives me a rectangle without any rounded corners.

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

回答by Geoff

While this question has been answered already (it's a bug that causes bottomLeftRadius and bottomRightRadius to be reversed), the bug has been fixed in android 3.1 (api level 12 - tested on the emulator).

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

So to make sure your drawables look correct on all platforms, you should put "corrected" versions of the drawables (i.e. where bottom left/right radii are actually correct in the xml) in the res/drawable-v12 folder of your app. This way all devices using an android version >= 12 will use the correct drawable files, while devices using older versions of android will use the "workaround" drawables that are located in the res/drawables folder.

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

回答by user256239

It looks like a bug http://code.google.com/p/android/issues/detail?id=939.

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

Finally I have to write something like this:

最后我必须写这样的东西:

 <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"/> 

I have to specify android:bottomRightRadius="2dp" for left-bottom rounded corner (another bug here).

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

回答by Entreco

From the documentation:

文档

NOTE:Every corner must (initially) be provided a corner radius greater than 1, or else no corners are rounded. If you want specific corners to not be rounded, a work-around is to use android:radius to set a default corner radius greater than 1, but then override each and every corner with the values you really want, providing zero ("0dp") where you don't want rounded corners.

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

E.g. you have to set an android:radius="<bigger than 1dp>"to be able to do what you want:

例如,您必须设置 anandroid:radius="<bigger than 1dp>"才能执行您想要的操作:

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

回答by Moncader

You can also use extremely small numbers for your radius'.

您还可以为半径使用极小的数字。

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

回答by Williams

for others there are a solution for any API level , you can place a item on top of each other example :

对于其他人,有任何 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>

the result with light color to show you the three items :

浅色结果显示三个项目:

enter image description here

在此处输入图片说明

the final result :

最终结果:

enter image description here

在此处输入图片说明

Best regards.

此致。

回答by Reaz Murshed

This bug is filed here. This is a bug of android devices having API level less than 12. You've to put correct versions of your layouts in drawable-v12 folder which will be used for API level 12 or higher. And an erroneous version(corners switched/reversed) of the same layout will be put in the default drawable folder which will be used by the devices having API level less than 12.

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

For example: I had to design a button with rounded corner at bottom-right.

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

In 'drawable' folder - button.xml:I had to make bottom-left corner rounded.

在“drawable”文件夹中 - button.xml:我必须使左下角变圆。

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

In 'drawable-v12' folder - button.xml:Correct version of the layout was placed here to be used for API level 12 or higher.

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

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

回答by Sai Gopi N

try this

尝试这个

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/upkia"/>
<corners android:radius="10dp"
    android:topRightRadius="0dp"
    android:bottomRightRadius="0dp" />
</shape>