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

样式效果图

1. shape属性

shape属性基本语法示例:

<?xml version="1.0" encoding="utf-8"?>
<shapexmlns: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(填充色)
    <solid>是用以指定内部填充色;
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#ffff00"/> //内部填充色
</shape>
  • gradient(渐变)
    <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"><strokeandroid: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>

2. shape用法

1)在res/drawable下新建shape_text.xml文件;

<?xml version="1.0" encoding="utf-8"?>
<shapexmlns: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" /><gradientandroid:startColor="#FF0000"android:endColor="#80FF00"android:angle="45"/> <padding android:left="10dp"android:top="10dp"android:right="10dp"android:bottom="10dp"/><sizeandroid: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>

如果觉得我的文章对你有帮助,请随意赞赏。您的支持将鼓励我继续创作!

作者:翻译不了的声响
链接:https://www.jianshu.com/p/70dc784a88d9
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

Android shape属性大全相关推荐

  1. Android Shape属性corners 圆角效果,边框效果...

    1,Corners [1]Corners标签是用来字义圆角的,其中radius与其它四个并不能共同使用. [2]android:radius:定义四个角的的圆角半径. [3]其它四个是逐个字义每个角的 ...

  2. android布局的属性大全,Android布局属性大全

    第一类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android:la ...

  3. Android布局属性大全

    第一类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android:la ...

  4. android shape大小,Android shape属性详解

    一.概述 最近太忙了,几乎每天都在做项目.同时写两个项目的感觉真爽. 在我们开发中,会经常遇到shape这种属性,这种属性可以在没有美工的情况照样可以实现我们想要的效果.自动动手,丰衣足食. 二.效果 ...

  5. Android xml 属性大全

    第一类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android:la ...

  6. Android Shape属性corners 圆角效果

    1,Corners [1]Corners标签是用来字义圆角的,其中radius与其它四个并不能共同使用. [2]android:radius:定义四个角的的圆角半径. [3]其它四个是逐个字义每个角的 ...

  7. android linearlayout属性大全,Android中LinearLayout布局的常用属性总结读书笔记

    8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? 原CSDN博客已弃用,文章会逐渐迁移过来. 应朋友们反馈的Android基础薄弱的问题,决定出一套Android基础教程 ...

  8. android radiobutton属性大全,Android RadioButton

    RadioButton 单选按钮有两种状态:选中或未选中.这允许用户从一个组中选择一个选项. RadioButton 属性 以下相关是 RadioButton 控件的重要属性.可以检查Android官 ...

  9. android linearlayout属性大全,LinearLayout 属性详解

    四个极其重要的参数,直接决定元素的布局和位置 android:layout_gravity 本元素相对于父元素的重力方向 android:gravity 本元素所有子元素的重力方向 android:o ...

  10. Android之shape属性详解

    有时候 ,为了满足一些需求,我们要用到 shape 去定义 一些背景,shape 的用法 跟图片一样 ,可以给View设置 Android:background="@drawable/sha ...

最新文章

  1. c语言模板程序,模板模式 (C语言实现)
  2. 机器学习——支持向量机SVM之非线性模型(低维到高维映射)
  3. redis源码剖析(十二)—— RDB持久化
  4. 漫谈SCA(软件成分分析)测试技术:原理、工具与准确性
  5. 微服务springCloud初识
  6. 大数据_Hbase_面试题0001
  7. UbuntuHelp:AptGet/Howto/zh
  8. 很好用的返回顶部代码
  9. 为什么网易云音乐总能知道你喜欢听什么歌?背后的原理竟然如此简单!
  10. Windows jdk下载与安装
  11. java+svm多分类器_svm多分类的java源码
  12. 外贸常用邮箱有哪些?163mail邮箱适合外贸用吗?
  13. 计算3个地理坐标点之间的夹角
  14. Android 7.0配置fileprovider共享文件 解决FileUriExposedException
  15. 【云原生】企业级容器管理平台Openshift介绍
  16. 第一篇博客------自我介绍
  17. linux运行md文件,Linux常用的18个命令.md
  18. 高效使用latex编辑数学公式
  19. FLEX 1.5 正式提供下载
  20. qt4.8+Phonon播放.wav声音文件

热门文章

  1. 全面解释java中StringBuilder、StringBuffer、String类之间的关系
  2. MapReduce模型、大数据与数据挖掘、云计算的关系
  3. 信息检索的基本方法(1)
  4. 购买地铁车票的规定如下: 乘1-4站,3元/位;乘5-9站,4元/位; 乘9站以上,5元/位。 输入乘坐人数(per_num)和乘坐站数(sta_num), 计算购买地铁车票需要的总金额,并将计算结果
  5. [JZOJ6080]【GDOI2019模拟2019.3.23】IOer【生成函数】【数学】
  6. 盘盘在项目中你常用的那些数组API
  7. 2022 各国程序员薪资大揭秘!
  8. linux系统如何安装bt5,BT5硬盘安装(多系统linux + win + BT5)
  9. 【民大Linux课件】Linux的基础操作1
  10. 手机的进化,离不开手机行业的“血海狂战”