Android-----使用Button特效selector+shape
2012-05-21 10:33
435 查看
如果对系统的ui效果不满意,可以用shape进行自定义
当然除了使用drawable这样的图片外今天谈下自定义图形shape的方法,对于button控件android上支持以下几种属性shape、gradient、stroke、corners等。
我们就以目前系统的button的selector为例说下:
对于上面,这条shape的定义,分别为渐变,在gradient中startcolor属性为开始的颜色,endcolor为渐变结束的颜色,下面的angle是角度。接下来是stroke可以理解为边缘,corners为拐角这里radius属性为半径,最后是相对位置属性padding。
对于一个button完整的定义可以为:
注意!提示大家,以上几个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" />
当然除了使用drawable这样的图片外今天谈下自定义图形shape的方法,对于button控件android上支持以下几种属性shape、gradient、stroke、corners等。
我们就以目前系统的button的selector为例说下:
<shape> <gradient android:startcolor="#ff8c00" android:endcolor="#ffffff" android:angle="270" /> <stroke android:width="2dp" android:color="#dcdcdc" /> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape>
对于上面,这条shape的定义,分别为渐变,在gradient中startcolor属性为开始的颜色,endcolor为渐变结束的颜色,下面的angle是角度。接下来是stroke可以理解为边缘,corners为拐角这里radius属性为半径,最后是相对位置属性padding。
对于一个button完整的定义可以为:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://www.norkoo.com">
<item android:state_pressed="true" >
<shape> <gradient android:startcolor="#ff8c00" android:endcolor="#ffffff" android:angle="270" /> <stroke android:width="2dp" android:color="#dcdcdc" /> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:startcolor="#ffc2b7"
android:endcolor="#ffc2b7"
android:angle="270" />
<stroke
android:width="2dp"
android:color="#dcdcdc" />
<corners
android:radius="2dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startcolor="#ff9d77"
android:endcolor="#ff9d77"
android:angle="270" />
<stroke
android:width="2dp"
android:color="#fad3cf" />
<corners
android:radius="2dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</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" />
相关文章推荐
- Android-----使用Button特效 selector+shape
- Android-----使用Button特效 selector+shape
- Android-----使用Button特效 selector+shape
- Android-----使用Button特效 selector shape
- 使用Button特效 selector+shape
- Android Button特效selector+shape
- Android Button特效selector+shape
- 使用Button特效 selector+shape
- Android Button的四种点击事件及RadioButton 的点击事件,背景选择器<selector>的使用
- android使用selector在 button上无效
- android shape selector layer-list使用说明
- android关于selector和shape的使用
- android selector shape 使用
- Android 文字阴影、layer-list、shape和selector使用
- 【Android进阶学习】shape和selector的结合使用
- Android中selector和shape的结合使用
- android中使用selector动态改变imageView或ImageViewButton的背景
- 【Android进阶学习】shape和selector的结合使用
- Android的selector和shape基本使用和color颜色列表
- [Android UI] shape和selector的结合使用