Android Shape设置任意个数圆角

Android Resource中有个shape类型得xml可以设置控件得形状,shape文件定义在drawable目录中,且shape标签必须为root element.官方推荐得标准写法为:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="float"
        android:centerY="float"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>

值得注意的是,在设置corners的时候,如果想要指定部分圆角,需要将所有的topLeftRadius,topRightRadius,bottomLeftRadius,bottomRightRadius放在一个corners里面,如果每个都放在一个corners里面,最下面的将覆盖最上面的。且:对于指定部分的圆角,官方文档上有说明,需要设置一个radius,并且值需要大于1,其它的如果想设置圆角的地方可以直接写自己想要的值,不想设置圆角的地方则设置为0dp,但是实际测试中发现,不需要设置radius也可以,(可能是测试的手机的api不对)。

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.

所以,在写shape的时候,需要注意的是将所有的圆角设置在一个corners里面,否则会被覆盖!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容