当然除了使用drawable这样的图片外今天谈下自定义图形shape的方法,对于Button控件Android上支持以下几种属性shape、gradient、stroke、corners等。

我们就以目前系统的Button的selector为例说下:

  1. <shape>
  2. <gradient
  3. android:startColor="#ff8c00"
  4. android:endColor="#FFFFFF"
  5. android:angle="270" />
  6. <stroke
  7. android:width="2dp"
  8. android:color="#dcdcdc" />
  9. <corners
  10. android:radius="2dp" />
  11. <padding
  12. android:left="10dp"
  13. android:top="10dp"
  14. android:right="10dp"
  15. android:bottom="10dp" />
  16. </shape>

对于上面,这条shape的定义,分别为渐变,在gradient中startColor属性为开始的颜色,endColor为渐变结束的颜色,下面的angle是角度。接下来是stroke可以理解为边缘,corners为拐角这里radius属性为半径,最后是相对位置属性padding。

对于一个Button完整的定义可以为:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector
  3. xmlns:android="http://www.norkoo.com">
  4. <item android:state_pressed="true" >
  5. <shape>
  6. <gradient
  7. android:startColor="#ff8c00"
  8. android:endColor="#FFFFFF"
  9. android:angle="270" />
  10. <stroke
  11. android:width="2dp"
  12. android:color="#dcdcdc" />
  13. <corners
  14. android:radius="2dp" />
  15. <padding
  16. android:left="10dp"
  17. android:top="10dp"
  18. android:right="10dp"
  19. android:bottom="10dp" />
  20. </shape>
  21. </item>
  22. <item android:state_focused="true" >
  23. <shape>
  24. <gradient
  25. android:startColor="#ffc2b7"
  26. android:endColor="#ffc2b7"
  27. android:angle="270" />
  28. <stroke
  29. android:width="2dp"
  30. android:color="#dcdcdc" />
  31. <corners
  32. android:radius="2dp" />
  33. <padding
  34. android:left="10dp"
  35. android:top="10dp"
  36. android:right="10dp"
  37. android:bottom="10dp" />
  38. </shape>
  39. </item>
  40. <item>
  41. <shape>
  42. <gradient
  43. android:startColor="#ff9d77"
  44. android:endColor="#ff9d77"
  45. android:angle="270" />
  46. <stroke
  47. android:width="2dp"
  48. android:color="#fad3cf" />
  49. <corners
  50. android:radius="2dp" />
  51. <padding
  52. android:left="10dp"
  53. android:top="10dp"
  54. android:right="10dp"
  55. android:bottom="10dp" />
  56. </shape>
  57. </item>
  58. </selector>

注意!提示大家,以上几个item的区别主要是体现在state_pressed按下或state_focused获得焦点时,当当来判断显示什么类型,而没有state_xxx属性的item可以看作是常规状态下。

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

<item

android:color="hex_color"

android:state_pressed=["true" | "false"]

android:state_focused=["true" | "false"]

android:state_selected=["true" | "false"]

android:state_active=["true" | "false"]

android:state_checkable=["true" | "false"]

android:state_checked=["true" | "false"]

android:state_enabled=["true" | "false"]

android:state_window_focused=["true" | "false"] />

</selector>

Elements:

<selector>

必须。必须是根元素。包含一个或多个<item>元素。

Attributes:

xmlns:android

String,必须。定义XML的命名空间,必须是

“http://schemas.android.com/apk/res/android”.

<item>

定义特定状态的color,通过它的特性指定。必须是<selector>的子元素。

Attributes:

android:color

16进制颜色。必须。这个颜色由RGB值指定,可带Alpha。

这个值必须以“#”开头,后面跟随Alpha-Red-Green-Blue信息:

l   #RGB

l   #ARGB

l   #RRGGBB

l   #AARRGGBB

android:state_pressed

Boolean。“true”表示按下状态使用(例如按钮按下);“false”表示非按下状态使用。

android:state_focused

Boolean。“true”表示聚焦状态使用(例如使用滚动球/D-pad聚焦Button);“false”表示非聚焦状态使用。

android:state_selected

Boolean。“true”表示选中状态使用(例如Tab打开);“false”表示非选中状态使用。

android:state_checkable

Boolean。“true”表示可勾选状态时使用;“false”表示非可勾选状态使用。(只对能切换可勾选—非可勾选的构件有用。)

android:state_checked

Boolean。“true”表示勾选状态使用;“false”表示非勾选状态使用。

android:state_enabled

Boolean。“true”表示可用状态使用(能接收触摸/点击事件);“false”表示不可用状态使用。

android:window_focused

Boolean。“true”表示应用程序窗口有焦点时使用(应用程序在前台);“false”表示无焦点时使用(例如Notification栏拉下或对话框显示)。

注意:记住一点,StateList中第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,这也是为什么默认的值必须总是在最后(如下面的例子所示)。

Examples:

XML文件保存在res/color/button_text.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"

android:color="#ffff0000"/> <!-- pressed -->

<item android:state_focused="true"

android:color="#ff0000ff"/> <!-- focused -->

<item android:color="#ff000000"/> <!-- default -->

</selector>

这个Layout XML会应用ColorStateList到一个View上:

<Button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/button_text"

android:textColor="@color/button_text" />

来源(一):http://www.norkoo.com/show/Mobile_Technology/android/haihkhjijkijajkihgcdkiibkbcee.aspx

来源(二):http://www.cnblogs.com/xirihanlin/archive/2010/06/11/1756121.html

Android-----使用Button特效 selector+shape的更多相关文章

  1. android 自定义Button,抛弃写shape文件

      标签: android 控件  自定义 2017年05月27日 17:52:13 611人阅读 评论(0) 收藏 举报 分类: 自定义View(2) 作者同类文章 X 版权声明:本文为博主原创文章 ...

  2. android自定义样式大全:shape,selector,layer-list,style,动画全部内容

    原文:http://keeganlee.me/post/android/20150830 以下摘取了部分内容: shape 一般用shape定义的xml文件存放在drawable目录下,若项目没有该目 ...

  3. android button的selector

    实现按钮的selector <?xml version="1.0" encoding="utf-8"?> <selector xmlns:an ...

  4. 浅谈Android样式开发之selector

    引言 上一篇Android UI中文章我们详细介绍了Android中shape标签的使用.通过shape标签我们可以定义矩形.椭圆.环形.直线等效果.不过shape只能定义单一的形状,在实际开发中,我 ...

  5. Android 自定义Button按钮显示样式(正常、按下、获取焦点)

    现在的用户对APP的外观看得很重要,如果APP内所有元件都用Android默认样式写,估计下面评论里就有一堆在骂UI丑的.今天学习自定义Button按钮样式.Button样式修改的是Button的背景 ...

  6. Android ImageButton | Button | TextView 点击和触摸效果

    ImageButton------------------------------------------------> 一.Java  代码: 在drawable目录下添加新的xml文件 bu ...

  7. Android:res之selector背景选择器

    selector根据不同的选定状态来定义不同的现实效果 常用属性: android:state_selected--------选中android:state_focused--------获得焦点a ...

  8. Android自定义Button的“款式”

    要想让你的button呈现出一种不一样的外观,一般会采取以下两种形式 采用selector里面加图片的方式 采用selector用shape进行代码控制的方式 对第一种方式而言,只需要注意好" ...

  9. Android样式的开发:selector篇

    上一篇详细讲了shape的用法,讲解了怎么用shape自定义矩形.圆形.线形和环形,以及有哪些需要注意的地方.不过,shape只能定义单一的形状,而实际应用中,很多地方比如按钮.Tab.ListIte ...

随机推荐

  1. cefSharp在XP下使得程序崩溃记录

    前言:这是一个奇葩的问题,到现在自己还没有搞明白问题出现在哪里,但是从问题总算是解决了,希望看到此文章的大牛,如果知道问题出在什么地方,可以告知一下. [一个在XP系统下面应用程序崩溃问题] 资源: ...

  2. 安卓 开发 The connection to adb is down, and a severe error has occured.

    The connection to adb is down, and a severe error has occured.问题解决 其原因就是其他进程占用了  ADB的端口,所以无法启动 遇到问题描 ...

  3. 数往知来 CSS<十二>

    div+css基础 一.外部样式<!--外部样式可以使网页与样式分离,分工处理 1.写网页,主要提供内容,一般都会有固定的结构,具有id等属性的标签包括特定的内容 2.根据结构写样式另存为css ...

  4. iOS App中数据加载的6种方式

    我们看到的APP,往往有着华丽的启动界面,然后就是漫长的数据加载等待,甚至在无网络的时候,整个处于不可用状态.那么我们怎么处理好界面交互中的加载设计,保证体验无缝衔接,保证用户没有漫长的等待感,而可以 ...

  5. [转] 編程風格要素-The Elements of Programming Style 中文英文中英對照

    转自: http://www.loliman3000.com/tech/2fe33ce32906f0302412881.php 下面的程序風格規則提煉自Brian Kernighan和P. J. Pl ...

  6. PYTHON压平嵌套列表

    list 是 Python 中使用最频繁的数据类型, 标准库里面有丰富的函数可以使用.不过,如果把多维列表转换成一维列表(不知道这种需求多不多),还真不容易找到好用的函数,要知道Ruby.Mathem ...

  7. [POJ] #1007# DNA Sorting : 桶排序

    一. 题目 DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95052   Accepted: 382 ...

  8. 在mac上搭建python环境

    原文出处:http://blog.justbilt.com/2014/07/02/setup_python_on_mac/ 这两天重新搞了下python的环境,发现好多地方还是容易忘记,因此有了这篇文 ...

  9. air 移动开发配置文件详解

    转自http://www.badyoo.com/index.php/2012/09/12/208/index.html 目录 所需的 AIR 运行时版本 应用程序标识 应用程序版本 主应用程序 SWF ...

  10. hdu5072-Coprime(容斥原理)

    题意:给N个互不相同的数,选择出两两互质或者两两不互质的三个数,有多少种选法. 题解:一共有C(N,3)中选择方式,减去不符合要求的,剩下的就是答案. 详见 http://blog.csdn.net/ ...