<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!--注意 android:background="@drawable/btn_selector"-->
<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/btn_selector"
android:text="@string/hello_world" />
</RelativeLayout>

drawabel下创建一个selector的xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button正常状态下的背景 -->
<item android:drawable="@drawable/btn_bg_normal"
android:state_pressed="false"/>
<!-- Button按下时的背景 -->
<item android:drawable="@drawable/btn_bg_pressed"
android:state_pressed="true"/>
</selector>

在drawable 目录下新建两个 shape文件分别是btn_bg_normal.xml,和btn_bg_pressed.xml文件

btn_bg_normal.xml


<?xml version="1.0" encoding="utf-8"?><!-- 按钮正常的时候的背景 --><!-- shape的默认形状是rectangle,还有oval(椭圆),line(线),ring(圆环),我就用过rectangle,其他的大家可以试一试 --><shape xmlns:android="http://schemas.android.com/apk/res/android"><!-- 矩形的圆角弧度 --><corners android:radius="10dp" /><!-- 矩形的填充色 --><solid android:color="#FF4081" /><!-- 矩形的边框的宽度,每段虚线的长度,和两段虚线之间的颜色和颜色 --><strokeandroid:width="1dp"android:dashWidth="8dp"android:dashGap="4dp"android:color="#4eb621" /></shape>

btn_bg_pressed.xml


<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"><!-- 矩形的圆角弧度 --><corners android:radius="10dp" /><!-- 矩形的填充色 --><solid android:color="#3F51B5" /><!-- 矩形的边框的宽度,每段虚线的长度,和两段虚线之间的颜色和颜色 --><strokeandroid:width="1dp"android:color="#4eb621"android:dashGap="4dp"android:dashWidth="8dp" /></shape>

注意 stroke 是指shape的边界线;

gradient是渐变线的api


angle:角度,当 android:type=“linear”时有效 ,亿45度为单位,逆时针方向旋转centerX:Float。渐变色中心的 X 相对位置( 0-1.0 )。当 android:type=“linear”时无效 centerY:Float。渐变色中心的 Y 相对位置( 0-1.0 )。当 android:type=“linear”时无效 centerColor:color。可选的颜色,出现在 start 和 end 颜色之间。 gradientRadius:Float。渐变色的半径。当 android:type=“radial” 时有效。 startcolor:开始的颜色 endcolor:结束的颜色 type:渐变色的样式。有效值为: “linear”:线性渐变,默认值 “radial”:环形渐变。 start 颜色是处于中间的颜色 “sweep”:扇形渐变 useLevel:Boolean。“ true ”表示可以当作 LevelListDrawable 使用(没搞懂是什么意思)

如果我们想给一个TextView 添加这样一个背景,一层背景是白色的,另外一层是蓝色的但是只露出来一部分,就可以使用layer-list实现。


<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" ><!--最近的项目中需要用到多个图层堆叠到一块儿,就研究了一下android中的layer-list。android中的layer-list就是用来多个图层堆叠显示的。 --><item><shape > <!-- 第一层的颜色为蓝色 --><solid android:color="@color/blue"/></shape></item><!-- 第二层的颜色,也就是最上面的一层,因为第二层相对于View的底部上移两个dp所以第一层会露出一个dp的黑色所以给人一种只有底部边框的假象 --><item android:bottom="2dip"><shape><solid android:color="@color/white"/></shape></item></layer-list>


<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android"><!--上面的为背景的底层--><item><shape android:shape="rectangle"><solid android:color="@color/colorPrimary" /></shape></item><!--背景上面的图层 让底部的背景露出来4dp的高度--><item android:bottom="4dp"><shape android:shape="rectangle"><solid android:color="@color/colorAccent" /></shape></item></layer-list>

效果图:


<?xml version="1.0dip" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" ><item><bitmap android:src="@drawable/ic_launcher"android:gravity="center"/></item><item android:left="10dip" android:top="10dip"><bitmap android:src="@drawable/ic_launcher"android:gravity="center"/></item><item android:left="20dip" android:top="20dip"><bitmap android:src="@drawable/ic_launcher"android:gravity="center"/></item><item android:left="30dip" android:top="30dip"><bitmap android:src="@drawable/ic_launcher"android:gravity="center"/></item></layer-list>

   效果图片

总结:

1、corners定义四个弧度:

radius 设置4个角的弧度、topLeftRadius设置左上角的弧度、topRightRadius设置右上角的弧度、bottomLeftRadius设置左下角的弧度、bottomRightRadius设置右下角的弧度

2、gradient渐变色填充

angle 渐变方向,逆时针

startColor 开始渐变色

centerColor 中间渐变色

endColor 最终渐变色

type 渐变的样式【linear' 线性】【radial默认是从中心开始的环形渐变效果】

gradientRadius 渐变的半径,只有type=radial有效

centerX 渐变开始的中心店x相对坐标(0-1.0),对type=linear有效

centerY渐变开始的中心店Y相对坐标(0-1.0),对type=linear有效

3、padding定义四个位置的内边距

left|top|right|bottom

4、size 定义图形的大小

5、solid 单色填充,里面只有一个color属性

6、stroke 为图形绘制边框

width 边框线的厚度

color 边框线的颜色

dashWidth 虚线的长度

dashGap 虚线之间的距离

shape的xml用法相关推荐

  1. shape的基本用法

    小源 我梦想那么大,我却离的那么远.... 目录视图 摘要视图 订阅 CSDN日报20170403 --<该不该离职?它说了算!>     程序员你的职场该这么规划!     Python ...

  2. shape的简单用法

    shap节点----------------------------------- 定义shape的值,必须是下面的之一: "rectangle" 矩阵,这也是默认的shape & ...

  3. crossdomain.xml用法

    使用crossdomain.xml让Flash可以跨域传输数据 本文来自http://www.mzwu.com/article.asp?id=975 一.概述 位于www.mzwu.com域中的SWF ...

  4. python 中 numpy 模块的 size,shape, len的用法

    numpy 中有很多类方法可以对数组处理,下面将介绍三种常见的处理数组的方法. 1.size的用法 import numpy as np X=np.array([[1,2,3,4],[5,6,7,8] ...

  5. Shape Drawable Xml的background

    2019独角兽企业重金招聘Python工程师标准>>> <?xml version="1.0" encoding="utf-8"?> ...

  6. linq To Xml 用法简介

    先看实例: 在配置文件中加入: <add key="SaveXmlPath" value="D:\SaveXml.xml"/> 而后创建一个实全类: ...

  7. Android开发(六)——组件颜色Selector(Selector与Shape的基本用法 )

    andorid控件改变状态时改变颜色,使用selector. <?xml version="1.0" encoding="utf-8" ?> < ...

  8. python的shape函数的用法

    shape函数是numpy.core.fromnumeric中的函数,它的功能是读取矩阵的长度,比如shape[0]就是读取矩阵第一维度的长度. shape的输入参数可以是一个整数(表示维度),也可以 ...

  9. python shape函数的用法

    shape[0]代表行数,shape[1]代表列数,shape是行和列数构成的元组 代码: import numpy as np x=np.array([[1,2,3],[2,3,5],[11,15, ...

最新文章

  1. 实心和空心哪个抗弯能力强_空心砖4大优缺点一次看 便宜耐用营造大自然原始风...
  2. Unbuntu18.04通过apt源方式安装mysql5.7.22
  3. Spring常用注解总结
  4. 小程序获取form_id 与 小程序获取openid
  5. Linux网络编程 之 广播(五)
  6. aix查看oracle数据库端口号,通过netstat+rmsock查找AIX端口对应进程
  7. JAVA常见业务参加解决方案_大话业务场景与解决方案-做任务
  8. python作品代码_学习python的一些心得体会
  9. paip.索引优化---sql distict—order by 法
  10. BZOJ2301[HAOI2011] Problem b
  11. Python 利用 shutil 移动文件
  12. 华为java面试题目,含面试题+答案
  13. 夏普ar2048s打印机驱动安装_驱动人生 下载安装打印机驱动的方法
  14. mysql_连接故障 Too many open files in system
  15. dirty cow的一点理解
  16. 11g rman 配置catalog
  17. 小爱同学点灯出现要操作的设备好像出问题了等一会再试吧的问题解决办法
  18. android企业实战视频培训班
  19. 发现微创软件在我心目中排全国第一的一个理由
  20. 计算机软件财务记账归入科目,糖酒会费用应计入什么科目

热门文章

  1. [转]Linux的tail 与head 命令
  2. hdu4410(Boomerang)(计算几何)
  3. Oracle的锁表与解锁
  4. 如何获取exe,dll中的图标以及源程序
  5. qq发文件大小上限_微信推出新功能!网友:终于不用转QQ了
  6. idea连接不了5.6mysql_IDEA无法连接mysql数据库的6种解决方法大全
  7. mysql架构 三级主从同步_MySQL 主从同步架构中你不知道的“坑”
  8. java在线学习系统源码_java学习成长之路(基础,源码,项目,实战)
  9. python绘制折线图先对数据进行处理_python气象数据分析并绘制折线图-女性时尚流行美容健康娱乐mv-ida网...
  10. 创意购物海报|吸引你的不止买买买,更是创意!