Android shape属性大全

在Android开发中,我们可以使用shape定义各种各样的形状,也可以定义一些图片资源。相对于传统图片来说,使用shape可以减少资源占用,减少安装包大小,还能够很好地适配不同尺寸的手机。

image.png

1. shape属性
shape属性基本语法示例:

<?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="integer"
        android:centerY="integer"
        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>

1)基本属性
Shape可以定义控件的一些展示效果,例如圆角,渐变,填充,描边,大小,边距;shape子标签就可以实现这些效果,shape子标签有下面几个属性:corners,gradient,padding,size,solid,stroke:

  • corners(圆角)
    <corners>是用来字义圆角;
<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <corners    //定义圆角   
    android:radius="10dp"  //全部的圆角半径;   
    android:topLeftRadius="5dp"  //左上角的圆角半径;   
    android:topRightRadius="5dp" //右上角的圆角半径;   
    android:bottomLeftRadius="5dp"  //左下角的圆角半径;   
    android:bottomRightRadius="5dp" /> //右下角的圆角半径。
</shape>    
  • solid(填充色)
    <gradient>用以定义渐变色,可以定义两色渐变和三色渐变,及渐变样式;
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <gradient  
    android:type=["linear" | "radial" | "sweep"]   //共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变;
    android:angle="90"  //渐变角度,必须为45的倍数,0为从左到右,90为从上到下;   
    android:centerX="0.5"  //渐变中心X的相当位置,范围为0~1;
    android:centerY="0.5"  //渐变中心Y的相当位置,范围为0~1;   
    android:startColor="#24e9f2"  //渐变开始点的颜色;   
    android:centerColor="#2564ef" //渐变中间点的颜色,在开始与结束点之间;   
    android:endColor="#25f1ef"  //渐变结束点的颜色;   
    android:gradientRadius="5dp"  //渐变的半径,只有当渐变类型为radial时才能使用;   
    android:useLevel="false" /> //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果。  
</shape> 
  • stroke(描边)
    <stroke>是描边属性,可以定义描边的宽度,颜色,虚实线等;
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <stroke        
    android:width="1dp"   //描边的宽度   
    android:color="#ff0000"   //描边的颜色   
    // 以下两个属性设置虚线   
    android:dashWidth="1dp" //虚线的宽度,值为0时是实线   
    android:dashGap="1dp" />//虚线的间隔
</shape> 
  • padding(内边距)
    <padding>是用来定义内部边距;
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <padding    
    android:left="10dp"  //左内边距; 
    android:top="10dp"   //上内边距;
    android:right="10dp"   //右内边距;
    android:bottom="10dp" /> //下内边距。
</shape>
  • size(大小)
    <size>标签是用来定义图形的大小的;
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <size   
    android:width="50dp"  //宽度 
    android:height="50dp" />// 高度
</shape>
  • 2)特殊属性
    Shape可以定义当前Shape的形状的,比如矩形,椭圆形,线形和环形;这些都是通过shape标签属性来定义的,shape标签有下面几个属性:**rectangle,oval,line,ring:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
  android:shape=["rectangle" | "oval" | "line" | "ring"] //shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)   
  //下面的属性只有在android:shape="ring"时可用:   
  android:innerRadius="10dp" //  内环的半径; 
  android:innerRadiusRatio="2"  // 浮点型,以环的宽度比率来表示内环的半径;   
  android:thickness="3dp"   // 环的厚度;   
  android:thicknessRatio="2" //  浮点型,以环的宽度比率来表示环的厚度;  
  android:useLevel="false"> //  boolean值,如果当做是LevelListDrawable使用时值为true,否则为false。
</shape>
  • rectangle(矩形)
<?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"/>
</shape>
  • oval(椭圆)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/colorPrimary"/>
    <size android:height="100dp"
        android:width="100dp"/>
</shape>
  • line(线)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="1dp"
         android:color="@color/colorAccent"
         android:dashGap="3dp"//虚线间距
         android:dashWidth="4dp"/>//虚线宽度
    <size android:height="3dp"/>
</shape>
  • ring(圆环)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:useLevel="false"
    android:innerRadius="20dp" // 内环的半径
    android:thickness="10dp"> // 圆环宽度
    <!--useLevel需要设置为false-->
    <solid android:color="@color/colorAccent"/>
</shape>
    1. shape用法
      1)在res/drawable下新建shape_text.xml文件;
<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners  
        android:radius="5dp"
        android:topLeftRadius="15dp"
        android:topRightRadius="15dp"
        android:bottomLeftRadius="15dp"
        android:bottomRightRadius="15dp" />

    <gradient
        android:startColor="#FF0000"
        android:endColor="#80FF00"
        android:angle="45"/> 

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

    <size
        android:width="200dp"
        android:height="200dp"/>

    <solid 
        android:color="#ffff9d"/>

    <stroke 
        android:width="2dp"
        android:color="#dcdcdc"/> 
</shape>

2)在布局中引用shape_text.xml文件;

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent">  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="Shape测试"  
        android:background="@drawable/shape_text"  
        android:textSize="15sp"  
        android:textColor="@android:color/black"/>  
</LinearLayout>

来源:Android shape属性大全

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,682评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,277评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,083评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,763评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,785评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,624评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,358评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,261评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,722评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,900评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,030评论 1 350
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,737评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,360评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,941评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,057评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,237评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,976评论 2 355

推荐阅读更多精彩内容