Android中drawable各个属性讲解
2016-05-21 15:13
519 查看
Android:drawable
放一个drawable资源
android:state_pressed 是否按下,如一个按钮触摸或者点击。
android:state_focused 是否取得焦点,比如用户选择了一个文本框。
android:state_hovered 光标是否悬停,通常与focused state相同,它是4.0的新特性
android:state_selected 被选中,它与focus state并不完全一样,如一个list view 被选中的时候,它里面的各个子组件可能通过方向键,被选中了。
android:state_checkable 组件是否能被check。如:RadioButton是可以被check的。
android:state_checked 被checked了,如:一个RadioButton可以被check了。
android:state_enabled 能够接受触摸或者点击事件
android:state_activated 被激活(这个麻烦举个例子,不是特明白)
android:state_window_focused 应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了
注意:如果有多个item,那么程序将自动从上到下进行匹配,最先匹配的将得到应用。(不是通过最佳匹配)
如果一个item没有任何的状态说明,那么它将可以被任何一个状态匹配。
详细的请看官方的API,那里写的更详细~!http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
通过Selector来设置按钮enable/unable状态的样式
我们可以用selector来配置button可用或者不可用时的背景,也可以用它来配置button不同状态下的文字颜色。下面左图是可用状态,右图是不可用状态。一、配置按钮不同状态的背景
首先我们准备下按钮的背景,这里是shape来做的。分为可用和不可用的背景,有蓝色(可用状态)和红色(不可用状态)两个背景。
btn_enabled_shape.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:radius="5dp" /> <solid android:color="#02a7e3" /> </shape>
btn_unabled_shape.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:radius="5dp" /> <solid android:color="#ff0000" /> </shape>
使用:
我们使用的时候只需要给button指定背景就好了。
android:background="@drawable/btn_bg_selector"
二、配置按钮不同状态下的文字颜色
我们首先要另外准备一个文件,这个文件中写上不同状态下的颜色信息。我把这个文件放在res/color/下
btn_text_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="true" android:color="#ffff0000"/> <item android:state_enabled="false" android:color="#ff0000ff"/> <!-- 默认样式 --> <item android:color="#ff00ff00"/> </selector>
使用:
使用时给button的textColor配置上这个文件就行了。
android:textColor="@color/btn_text_selector"
源码下载:http://download.csdn.net/detail/shark0017/8452377
相关文章推荐
- Android编程中TextView宽度过大导致Drawable无法居中问题解决方法
- 详解Android中Drawable方法
- Android开发使用Drawable绘制圆角与圆形图案功能示例
- Android Bitmap和Drawable相互转换的简单代码
- xml中一些关于设置的细节(简单)
- android 各种控件颜色值的设置(使用Drawable,Color)
- android中Drawable总结
- Android中Bitmap,byte[],Drawable相互转化
- android 一些数据转换方法
- mipmap和drawable文件夹的区别
- Android之drawable state各个属性详解
- android基础之drawable
- Android Bitmap 与 Drawable之间的区别和转换
- 安卓 Drawable源码分析简述
- Drawable Resources
- Android系统drawable浅谈
- Drawable、Bitmap、Canvas和Paint的关系
- Android优化系列——性能优化(资源存储)
- 判断两个安卓Drawable图片是否为同一图片
- 用Drawable来实现UI元素的间隔