介绍

简单来说,shape就是用来在xml文件中定义形状,代码解析之后就可以当做Drawable一样使用

官方说明

关于shape定义的drawable

文件位置:res/drawable/filename.xml

编译资源类型:GradientDrawable

文件引用:

In Java: R.drawable.filename

In XML: @[package:]drawable/filename

语法:

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

这里只做简单的描述,主要看看使用方式。关于元素的详细说明,请看 shape说明

实际使用

矩形

填充(solid)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--填充颜色 -->
<solid android:color="@color/colorAccent" />
</shape>


描边(stroke)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> <!--填充颜色 -->
<solid android:color="@color/colorAccent" /> <!--描边颜色-->
<!--android:dashGap 虚线间距,这里设置为0则显示的为实线-->
<!--android:dashWidth 虚线宽度-->
<stroke
android:width="3dp"
android:color="@color/colorPrimaryDark"
android:dashGap="0dp"
android:dashWidth="10dp" />
</shape>


圆角(corner)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> <!--填充颜色 -->
<solid android:color="@color/colorAccent" /> <!--描边颜色-->
<!--android:dashGap 虚线间距,这里设置为0则显示的为实线-->
<!--android:dashWidth 虚线宽度-->
<stroke
android:width="3dp"
android:color="@color/colorPrimaryDark"
android:dashGap="4dp"
android:dashWidth="10dp" />
<!--圆角-->
<!--android:randius 设置4个叫的圆角半径,会被特定的圆角设定覆盖-->
<!--android:bottomLeftRadius 左下角的圆角半径-->
<!--android:bottomRightRadius 右下角的圆角半径-->
<!--android:topLeftRandius 左上角的圆角半径-->
<!--android:topRightRadius 右上角的圆角半径-->
<corners
android:bottomLeftRadius="60dp"
android:radius="30dp"
android:topRightRadius="120dp" />
</shape>


渐变(gradient):linear

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> <!--描边颜色-->
<!--android:dashGap 虚线间距,这里设置为0则显示的为实线-->
<!--android:dashWidth 虚线宽度-->
<stroke
android:width="3dp"
android:color="@color/colorPrimaryDark"
android:dashGap="4dp"
android:dashWidth="10dp" />
<!--圆角-->
<!--android:randius 设置4个叫的圆角半径-->
<!--android:bottomLeftRadius 左下角的圆角半径-->
<!--android:bottomRightRadius 右下角的圆角半径-->
<!--android:topLeftRandius 左上角的圆角半径-->
<!--android:topRightRadius 右上角的圆角半径-->
<corners
android:bottomLeftRadius="60dp"
android:radius="30dp"
android:topRightRadius="120dp" />
<!--渐变-->
<gradient
android:angle="45"
android:centerColor="@color/stone"
android:endColor="@color/pink"
android:startColor="@color/yellow" /> </shape>

渐变(gradient):radial

<gradient
android:angle="90"
android:startColor="@color/colorPrimary"
android:centerColor="@color/pink"
android:endColor="@color/yellow"
android:gradientRadius="400dp"
android:type="radial"/>

渐变(gradient):sweep

<gradient
android:startColor="@color/colorPrimary"
android:centerColor="@color/pink"
android:endColor="@color/yellow"
android:gradientRadius="400dp"
android:type="sweep"/>


圆形

正圆

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:centerColor="@color/pink"
android:endColor="@color/yellow"
android:startColor="@color/colorPrimary" />
<size
android:width="400dp"
android:height="400dp" />
</shape>


椭圆

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:angle="90"
android:centerColor="@color/pink"
android:endColor="@color/yellow"
android:startColor="@color/colorPrimary" />
</shape>


线条

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:width="9dp"
android:color="@color/pink" />
</shape>


环形

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadius="100dp"
android:thickness="50dp"
android:useLevel="false"
>
<gradient android:startColor="@color/colorAccent"
android:endColor="@color/yellow"
android:centerColor="@color/pink"/>
</shape>

android:useLevel=”false”这个属性值一定要设置成false,根据google官网上的解释:

以下属性只能在android:shape="ring"的时候使用:

属性 意义
android:innerRadius 尺寸,内环的半径
android:thickness 尺寸,环的厚度
android:innerRadiusRatio 浮点型,以环的宽度比率来表示内环的半径, 例如,如果android:innerRadiusRatio=5,表示内环半径等于环的宽度除以5,这个值是可以被android:innerRadius的值覆盖,默认为9.
android:thicknessRatio 浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio=2, 那么环的厚度就等于环的宽度除以2。这个值是可以被android:thickness覆盖的,默认值是3.
android:useLevel boolean值,如果当做是LevelListDrawable使用时值为true,否则为false.这个值一般为false,否则你的环可能不会出现

其他说明

这些自己定义的shape为根节点的drawable xml文件,可以用来当成背景使用在Button,TextView等视图

上,同时由于可以设置size大小,也可以用来制作简单的图标等。总而言之,每个细小的东西,都有挖掘的

价值,感觉这里面还有一些东西我没有注意到,还要好好的看一下文档。

最后,google镜像网站,xsoftlab,当然有条件的建议使用Google官网。

<Android 基础(十三)> shape的更多相关文章

  1. Android 基础:常用布局 介绍 & 使用(附 属性查询)

    Android 基础:常用布局 介绍 & 使用(附 属性查询)   前言 在 Android开发中,绘制UI时常需各种布局 今天,我将全面介绍Android开发中最常用的五大布局 含 Andr ...

  2. Android基础测试题(四)

    看了前两道题大家有没有发现,测试题少了(一),大家猜猜测试题(一)是什么? Android基础测试题(四): 需求: 建一个方法,格式化输出2016-11-14 10:15:26格式的当前时间,然后截 ...

  3. Android基础测试题(二)

    今天给大家带来的是Android基础测试题(二) 题目要求: 定义一个5位长度的整型数组并初始化,然后构建方法根据用户传入的数字判断是否存在数组中,如果存在,返回所在位置,如果不存在,返回-1 首先第 ...

  4. Bootstrap<基础十三> 按钮组

    按钮组允许多个按钮被堆叠在同一行上.当你想要把按钮对齐在一起时,这就显得非常有用.你可以通过Bootstrap 按钮(Button) 插件 添加可选的 JavaScript 单选框和复选框样式行为. ...

  5. Mono.Android 基础

    Mono.Android 基础 (地址) Mono.Android项目结构是 — Project + Assets + Resources + drawable + layout + values R ...

  6. Android背景渐变色(shape,gradient)

    Android设置背景色可以通过在res/drawable里定义一个xml,如下: <?xml version="1.0" encoding="utf-8" ...

  7. android 开发:shape和selector和layer-list的(详细说明)

    目录(?)[+] Shape 简介 使用的方法 属性 Selector 简介 使用的方法 layer-list 简介 例子 最后   <shape>和<selector>在An ...

  8. 深入理解gradle编译-Android基础篇

    深入理解gradle编译-Android基础篇 导读 Gradle基于Groovy的特定领域语言(DSL)编写的一种自动化建构工具,Groovy作为一种高级语言由Java代码实现,本文将对Gradle ...

  9. android基础---->JSON数据的解析

    上篇博客,我们谈到了XML两种常用的解析技术,详细可以参见我的博客(android基础---->XMl数据的解析).网络传输另外一种数据格式JSON就是我们今天要讲的,它是比XML体积更小的数据 ...

随机推荐

  1. Index Filter及ICP特性

    原文链接:http://hedengcheng.com/?p=577 (一)问题描述 一条SQL,在数据库中是如何执行的呢?相信很多人都会对这个问题比较感兴趣.当然,要完整描述一条SQL在数据库中的生 ...

  2. #6145. 「2017 山东三轮集训 Day7」Easy 动态点分治

    \(\color{#0066ff}{题目描述}\) JOHNKRAM 最近在参加 C_SUNSHINE 举办的聚会. C 国一共有 n 座城市,这些城市由 n−1 条无向道路连接.任意两座城市之间有且 ...

  3. 「模拟赛20181025」御风剑术 博弈论+DP简单优化

    题目描述 Yasuo 和Riven对一排\(n\)个假人开始练习.斩杀第\(i\)个假人会得到\(c_i\)个精粹.双方轮流出招,他们在练习中互相学习,所以他们的剑术越来越强.基于对方上一次斩杀的假人 ...

  4. Qt 学习之路 2(15):标准对话框 QMessageBox

    Qt 学习之路 2(15):标准对话框 QMessageBox  豆子  2012年9月18日  Qt 学习之路 2  40条评论 所谓标准对话框,是 Qt 内置的一系列对话框,用于简化开发.事实上, ...

  5. csv HTTP简单表服务器

    HTTP Simple Table Server Download Performance testing with JMeter can be done with several JMeter in ...

  6. C语言值拷贝传递机制

    当参数是常量,变量,或表达式时,传递的数据就是这些数据对象所具有的内容,这种方式称为数值参数传递方式(简称传值方式).如果函数调用时所传递的实参是数据对象在内存中的存储单元的首地址值,这种方式称为地址 ...

  7. Mysql 在DOS窗口下的操作

    1.登录 (1)首先cd 进入mysql对应的安装目录 (2)再输入mysql -u root -p 2.重启 https://blog.csdn.net/u012385190/article/det ...

  8. 数据恢复(Data recovery)

    定义数据恢复: 当存储介质出现损伤或由于人员误操作.操作系统故障本身故障所造成的数据不可见,无法读取.丢失. 工程师通过特殊的手段读取却在正常状态下不可见,不可读,无法读的数据. 数据恢复(Data ...

  9. 修改阿里云ESC Centos 7.4 防火墙开放端口

    例如系统:Centos 7.4操作如下 1,进入 cd /etc/firewalld/zones/ 目录 2,编辑 vim public.xml 3,按i或insert键进入编辑模式 4,在<z ...

  10. 单元测试mock框架——jmockit实战

    JMockit是google code上面的一个java单元测试mock项目,她很方便地让你对单元测试中的final类,静态方法,构造方法进行mock,功能强大.项目地址在:http://jmocki ...