android shape.xml 属性详解

转载源:http://blog.csdn.net/harvic880925/article/details/41850723

一、简单使用

刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标签怎么用。

1、新建shape文件

首先在res/drawable文件夹下,新建一个文件,命名为:shape_radius.xml

内容是这样的:(先不需要理解,先看shape怎么用)

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <corners android:radius="20dip"/>  
  4.     <solid android:color="#ff00ff"/>  
  5.   
  6. </shape>  

2、添加到控件中

在定义好shape文件后,下一步就是将其添加到控件中,添加到控件中,一般是使用设置background属性,将其为控件背景,下面,我们将其设置为MainActivity对应的布局中(activity_main.xml),将其设为TextView的背景,看显示出来 是什么样子的。

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="com.harvic.tryshape.MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_margin="50dip"  
  11.         android:text="@string/hello_world"   
  12.         android:background="@drawable/shape_radius"/>  
  13.       
  14. </RelativeLayout>  

显示出来的结果是这样的:



二、基本属性(corners、gradient、padding、size、solid、stroke)

上面给大家简单的讲了下shape标签组的简单使用方法,下面就具体讲讲shape标签里所具有的几个子标签及所具有的属性。

1、Corners

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <corners    //定义圆角    
  2.     android:radius="dimension"      //全部的圆角半径    
  3.     android:topLeftRadius="dimension"   //左上角的圆角半径    
  4.     android:topRightRadius="dimension"  //右上角的圆角半径    
  5.     android:bottomLeftRadius="dimension"    //左下角的圆角半径    
  6.     android:bottomRightRadius="dimension" />    //右下角的圆角半径    

Corners标签是用来字义圆角的,其中radius与其它四个并不能共同使用。

Android:radius:定义四个角的的圆角半径。

其它四个是逐个字义每个角的圆角半径。

使用:

控件布局:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent" >  
  4.   
  5.     <TextView  
  6.         android:layout_width="100dp"  
  7.         android:layout_height="100dp"  
  8.         android:layout_margin="50dip"  
  9.         android:text="@string/hello_world"   
  10.         android:background="@drawable/shape_radius"/>  
  11.  </RelativeLayout>  

shape定义:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <corners android:radius="20dip"/>  
  4.     <solid android:color="#ffff00"/>  
  5. </shape>  

效果:



2、solid

solid用以指定内部填充色

只有一个属性:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <solid  android:color="color" />    

在上面的例子中,我们就将填充色指定为#ffff00了,如果我们不加圆角,只使用填充色,即将shape变成这样子:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <solid android:color="#ffff00"/>  
  4. </shape>  

那效果就是这样的:



3、gradient

gradient用以定义渐变色,可以定义两色渐变和三色渐变,及渐变样式,它的属性有下面几个:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <gradient   
  2.     android:type=["linear" | "radial" | "sweep"]    //共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变    
  3.     android:angle="integer"     //渐变角度,必须为45的倍数,0为从左到右,90为从上到下    
  4.     android:centerX="float"     //渐变中心X的相当位置,范围为0~1    
  5.     android:centerY="float"     //渐变中心Y的相当位置,范围为0~1    
  6.     android:startColor="color"   //渐变开始点的颜色    
  7.     android:centerColor="color"  //渐变中间点的颜色,在开始与结束点之间    
  8.     android:endColor="color"    //渐变结束点的颜色    
  9.     android:gradientRadius="float"  //渐变的半径,只有当渐变类型为radial时才能使用    
  10.     android:useLevel=["true" | "false"] />  //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果    

首先有三种渐变类型,分别是:linear(线性渐变)、radial(放射性渐变)、sweep(扫描式渐变)

(1)先看看这几个属性的使用方法:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. android:type=["linear" | "radial" | "sweep"]  
  2. android:startColor="color"   //渐变开始点的颜色    
  3. android:centerColor="color"  //渐变中间点的颜色,在开始与结束点之间    
  4. android:endColor="color"    //渐变结束点的颜色    
  5. android:gradientRadius="float"  //渐变的半径,只有当渐变类型为radial时才能使用    

下面我们使用三色渐变来看看这三种渐变方式都是怎么显示的:(如果不使用centerColor属性就是双色渐变,这个属性是可选的)


需要注意的一点是,在构造放射性渐变时,要加上android:gradientRadius属性(渐变半径),即必须指定渐变半径的大小才会起作用,下面列出这三个渐变方式的shape代码,供大家参考:

线性渐变:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <gradient   
  4.         android:type="linear"  
  5.         android:startColor="#ff0000"  
  6.         android:centerColor="#00ff00"  
  7.         android:endColor="#0000ff"/>  
  8. </shape>  

放射性渐变:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <gradient   
  4.         android:type="radial"  
  5.         android:startColor="#ff0000"  
  6.         android:centerColor="#00ff00"  
  7.         android:endColor="#0000ff"  
  8.         android:gradientRadius="100"/>  
  9. </shape>  

扫描式渐变:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <gradient   
  4.         android:type="sweep"  
  5.         android:startColor="#ff0000"  
  6.         android:centerColor="#00ff00"  
  7.         android:endColor="#0000ff"/>  
  8. </shape>  

可见放射性渐变,只是比其它两个多了个android:gradientRadius属性

(2)、android:angle属性(仅对线性渐变有效)

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. android:angle="integer"     //渐变角度,必须为45的倍数,0为从左到右,90为从上到下    

我们在上面的三种渐变上都加上angle属性,看看效果如何:


能过跟上一个图对比可以发现,angle属性确实只对线性渐变有效,其它两种渐变方式都没有任何动静,下面是此时的线性渐变shape代码:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <gradient   
  4.         android:type="linear"  
  5.         android:startColor="#ff0000"  
  6.         android:centerColor="#00ff00"  
  7.         android:endColor="#0000ff"  
  8.         android:angle="45"/>  
  9. </shape>  

(3)、android:centerX与android:centerY

centerX、centerY两个属性用于设置渐变的中心点位置,仅当渐变类型为放射渐变时有效,类型为分数或小数,不接受Dimension。默认值是0.5,有效值是0.0~1.0,超出该范围后会看不出渐变效果。centerX、centerY的取值其实是宽和高的百分比;不难理解,下面看代码:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <gradient   
  4.         android:type="sweep"  
  5.         android:startColor="#ff0000"  
  6.         android:centerColor="#00ff00"  
  7.         android:endColor="#0000ff"  
  8.         android:centerX="0.2"  
  9.         android:centerY="0.8"/>  
  10. </shape>  

取宽度的20%和高度的80%的位置,作为新的渐变原点,效果是这样的:


(4)android:useLevel

useLevel属性通常不使用。该属性用于指定是否将该shape当成一个LevelListDrawable来使用,默认值为false。

4、stroke

这是描边属性,可以定义描边的宽度,颜色,虚实线等

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <stroke         
  2.     android:width="dimension"   //描边的宽度    
  3.     android:color="color"   //描边的颜色    
  4.     // 以下两个属性设置虚线    
  5.     android:dashWidth="dimension"   //虚线的宽度,值为0时是实线    
  6.     android:dashGap="dimension" />      //虚线的间隔   

上面各个属性的意义如下:

我们使用绿色虚线描边,虚线高度是20dp,虚线宽度为10dp虚线间距为1dp:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <stroke   
  4.         android:width="20dp"   
  5.         android:color="#00ff00"  
  6.         android:dashWidth="10dp"  
  7.         android:dashGap="1dp" />  
  8. </shape>  

从效果图中,我们也能清晰的看出这三个参数(width、dashwidth、dashGap)之间的区别:

5、size和padding

这两个基本上不怎么用,因为他们所具有的功能,控件本身也能实现。
size:是用来定义图形的大小的。

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <size    
  2.     android:width="dimension"    
  3.     android:height="dimension" />  

padding:用来定义内部边距

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <padding     
  2.     android:left="dimension"    
  3.     android:top="dimension"    
  4.     android:right="dimension"    
  5.     android:bottom="dimension" />  

 

三、Shape的属性(rectangle、oval、line、ring)

上面我们讲了Shape的子标签的的作用,但Shape本身还没讲,Shape自已是可以定义当前Shape的形状的,比如上面的矩形,还有椭圆形,线形和环形;这些都是通过Shape标签的 shape属性来定义的,Shape标签总共有下面几个属性,我们一个个讲:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. android:shape=["rectangle" | "oval" | "line" | "ring"]    
  2. shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)    
  3. 下面的属性只有在android:shape="ring时可用:    
  4. android:innerRadius         尺寸,内环的半径。    
  5. android:innerRadiusRatio    浮点型,以环的宽度比率来表示内环的半径,    
  6. android:thickness           尺寸,环的厚度    
  7. android:thicknessRatio      浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio="2",    
  8. android:useLevel            boolean值,如果当做是LevelListDrawable使用时值为true,否则为false.   

可见,只有第一个shape是可用的,其它五个都是shape等于ring时所特有的。

注意,无论这里shape取什么形状,他的子标签都是可用的,但有时并不会有效果,比如在shape为椭圆时,那corners标签就不会有效果,很显然的道理。下面一个个看看各个形状都是怎么样的;

1、rectangle (矩形)

这就是上一节我们使用的形状,当我们不指定shape属性时,默认就是矩形的。 

控件代码:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="horizontal" >      
  5.     <TextView  
  6.         android:layout_width="300dp"  
  7.         android:layout_height="100dp"  
  8.         android:layout_margin="10dp"  
  9.         android:textColor="#ffffff"  
  10.         android:textSize="18sp"  
  11.         android:textStyle="bold"  
  12.         android:background="@drawable/try_shape_3"/>  
  13.  </LinearLayout>  

shape代码:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:shape="rectangle">  
  4.     <solid android:color="#ff00ff"/>  
  5. </shape>  

对应图形:

2、oval(椭圆)

控件代码不变,下面是shape代码:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:shape="oval">  
  4.     <solid android:color="#ff00ff"/>  
  5. </shape>  

对应图形:(控件大小的矩形所对应的椭圆)


3、line(线形)

没觉得这个能有什么用……,也不讲了,没什么意思

4、ring(环形)

还记得他所特有的几个属性么:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. android:innerRadius         尺寸,内环的半径。    
  2. android:thickness           尺寸,环的厚度    
  3. android:innerRadiusRatio    浮点型,以环的宽度比率来表示内环的半径,    
  4.       例如,如果android:innerRadiusRatio,表示内环半径等于环的宽度除以5,这个值是可以被覆盖的,默认为9.    
  5. android:thicknessRatio      浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio="2",    
  6.       那么环的厚度就等于环的宽度除以2。这个值是可以被android:thickness覆盖的,默认值是3.    
  7. android:useLevel            boolean值,如果当做是LevelListDrawable使用时值为true,否则为false.  

这么几个属性无外乎就是定义环形的内环尺寸和环的宽度。

举个例子:

控件定义:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="horizontal" >      
  5.     <TextView  
  6.         android:layout_width="300dp"  
  7.         android:layout_height="100dp"  
  8.         android:layout_margin="10dp"  
  9.         android:textColor="#ffffff"  
  10.         android:textSize="18sp"  
  11.         android:textStyle="bold"  
  12.         android:background="@drawable/try_shape_2"/>  
  13.  </LinearLayout>  

shape定义:(这里一定要要加上useLevel属性并定义为false,不然没有效果)

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:shape="ring"   
  4.     android:innerRadius="20dp"   
  5.     android:thickness="50dp"    
  6.     android:useLevel="false">  
  7.       
  8.     <solid android:color="#ff00ff"/>  
  9.       
  10. </shape>  

效果图:



源码地址:http://download.csdn.net/detail/harvic880925/8249629
请大家尊重原创者版权,转载请标时出处:http://blog.csdn.net/harvic880925/article/details/41850723 谢谢。

转载于:https://www.cnblogs.com/imqsl/p/6561173.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541737.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux检查防火墙是否阻挡端口,浅析linux查看防火墙状态和对外开放的端口状态...

1.查看防火墙状态查看防火墙状态 systemctl status firewalld开启防火墙 systemctl start firewalld关闭防火墙 systemctl stop firewalld开启防火墙 service firewalld start若遇到无法开启先用&#xff1a;systemctl unmask firewalld.service然后&#xff1a;systemctl star…

Java类class getClasses()方法及示例

类的类getClasses()方法 (Class class getClasses() method) getClasses() method is available in java.lang package. getClasses()方法在java.lang包中可用。 getClasses() method is used to return an array that contains Class objects denoting all the public classes…

linux内核计数函数,linux中的内核引用计数器

linux中的内核引用计数器文档 /Documentation/kref.txt翻译。krefs能让你往你的对象中添加一个引用计数器。如果你有一些需要在多处被使用和传递的对象&#xff0c;而你并没有给这些对象中添加引用计数器的话&#xff0c;你的代码肯定会有某些缺陷&#xff0c;会出现一些问题。…

jQuery常用的全局方法源码

下面常用方法的详细使用请查看&#xff1a;http://www.cnblogs.com/moqiutao/p/4775725.html 1.$.noConflict()方法 语法&#xff1a;jQuery.noConflict(removeAll) removeAll&#xff1a;布尔值。指示是否允许彻底将 jQuery 变量还原。 源码&#xff1a; var// Map over jQuer…

isinstance_Java类class isInstance()方法及示例

isinstance类class isInstance()方法 (Class class isInstance() method) isInstance() method is available in java.lang package. isInstance()方法在java.lang包中可用。 isInstance() method is used to check whether the given object is an instance with the object d…

Linux比较大文件内容,Linux系统最大文件打开数优化,解决Too many open files报错

这是一个Linux系统常见的故障&#xff0c;网络上也能轻易的找到解决办法&#xff0c;我也只是在工作中遇到了这个问题&#xff0c;所以在博客记录下&#xff0c;以备不时之需。一、报错截图&#xff1a;图为resin的报错日志&#xff0c;很明显提示了Too many open files&#x…

java日历类add方法_Java日历computeFields()方法及示例

java日历类add方法日历类的computeFields()方法 (Calendar Class computeFields() method) computeFields() method is available in java.util package. 在java.util包中提供了validateFields()方法 。 computeFields() method is used to convert current ms(milliseconds) t…

Varnish缓存代理简介与配置

一、varnish原理&#xff1a;1&#xff09;Varnish简介&#xff1a;varnish缓存是web应用加速器&#xff0c;同时也作为http反向缓存代理。你可以安装varnish在任何http的前端&#xff0c;同时配置它缓存内容。与传统的 squid 相比&#xff0c;varnish 具有性能更高、速度更快、…

Linux允许61440端口,释放对某端口的占用

释放对某端口的占用假如我们需要确定谁占用了我们的9050端口在windows命令行窗口下执行&#xff1a;1.查看所有的端口占用情况C:\>netstat -ano协议 本地地址 外部地址 状态 PIDTCP 127.0.0.1:1434 0.0.…

as_hash ruby_Hash.merge(other_hash)方法与Ruby中的示例

as_hash rubyHash.merge(other_hash)方法 (Hash.merge(other_hash) Method) In this article, we will study about Hash.merge(other_hash) Method. The working of the method can’t be assumed because it’s quite a different name. Let us read its definition and unde…

linux 安装nfs 客户端,在CentOS 7上安装NFS服务器和客户端

NFS服务器和客户端安装在CentOS 7上版本1.0作者&#xff1a;Srijan Kishore 在Twitter上关注howtoing最后编辑 16 / Dec / 2014本指南介绍如何在CentOS 7.0中配置NFS服务器网络文件系统(NFS)是一种流行的分布式文件系统协议&#xff0c;可让用户在其服务器上安装远程目录。 该系…

安装ORACLE 时报错 /jre/1.4.2/lib/i386/libawt.so:

最近在linux下安装oracle 10g时&#xff0c;碰到如下问题&#xff1a; /tmp/OraInstall2011-09-11_02-16-11PM/jre/1.4.2/lib/i386/libawt.so: libXp.so.6: cannot open shared object file: No such file or directory occurred.. 网上找了下&#xff0c;真让人费解呀&am…

Java线程start()vs run()方法及示例

Java | 线程start()vs run()方法 (Java | Thread start() vs run() Methods) When we call the start() method, it leads to the creation of a new thread. Then, it automatically calls the run() method. If we directly call the run() method, then no new thread will …

linux安装卸载mysql,Linux6 系列 安装、卸载mysql

Linux6 系列 安装、卸载mysqlLinux6 系列 安装、卸载mysqlLinux环境下载mysql&#xff1a;https://blog.csdn.net/weixin_40816738/article/details/90111456一、安装环境依赖&#xff1a;yum install -y cmake make gcc gcc-c libaio ncurses ncurses-devel二、安装流程1、软件…

Python | 如何使用pip升级所有Python软件包?

While using Python as a programming language, its a very common scenario to use a virtual environment and PIP, a package manager for python. 当使用Python作为编程语言时&#xff0c;使用虚拟环境和PIP (Python的程序包管理器)是一种非常常见的情况。 Its a common …

linux下enum类型占几个字节,enum大小问题

问题描述板卡有两个CPU&#xff0c;ARMMIPS&#xff0c;同时运行三个系统REE(linux) TEE(SierraTEE) SEE(TDS)。TEE跟SEE通过RPC进行通信&#xff0c;有enum成员的结构体信息传递会出错&#xff0c;如下结构体&#xff1a;struct sTag {enum A;enum B;int C;enum D;};问题分析…

ASP.NET导出word实例

ASP.NET导出word实例 最近遇到一个题目就是如何在asp.net中将数据导出到word中&#xff0c;由于数据是动态的&#xff0c;所以需要在后台拼出想要的的格式&#xff0c;翻遍了网页找出了一个比较满意的代码&#xff0c;感谢那位高手。代码如下&#xff1a; public void Download…

Java LocalDate类| toString()方法与示例

LocalDate类toString()方法 (LocalDate Class toString() method) toString() method is available in java.time package. toString()方法在java.time包中可用。 toString() method is used to represent this LocalDate as a String by using the standards ISO-8601 format.…

linux14.04 Apache,Ubuntu 14.04编译安装Apache

Ubuntu下编译安装apache需要预先编译安装多个依赖件&#xff0c;包括&#xff1a;apr, apr-util,pcre,zlib-devel&#xff0c;等&#xff0c;相当麻烦&#xff0c;记录于此备查.由于Ubuntu系统默认安装时没有安装C&#xff0c;所以也需要先安装c编译需要相关的组件。[注]apt-ca…

Android Jenkins自动化构建之路

install Jenkins 添加Jenkins的源&#xff08;repository&#xff09;: sudo wget -O /etc/yum.repos.d/jenkins.repo http://jenkins-ci.org/redhat/jenkins.repo sudo rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key复制代码yum install Jenkins复制代码…