StateListDrawable可以根据View的不同状态,更换不同的背景

可以应用如EditText,Button等中,以Button为例

系统中默认的按钮被按下的颜色和未点击时的颜色不一样,该种实现可以用Java代码和XML实现

以Java代码:

//……前面对Button的声明略去
okBtn.setBackgroundDrawable(addStateDrawable(this, R.drawable.btn_normal, R.drawable.btn_selected, R.drawable.btn_selected));
cancelBtn.setBackgroundDrawable(addStateDrawable(this, R.drawable.btn_normal, R.drawable.btn_selected, R.drawable.btn_selected)); //……对应主要的代码
//当对应的View处于不同的状态时,对应的bacdground跟着变化
private StateListDrawable addStateDrawable(Context context, int idNormal, int idPressed, int idFocused) {
StateListDrawable sd = new StateListDrawable();
Drawable normal = idNormal == - ? null : context.getResources().getDrawable(idNormal);
Drawable pressed = idPressed == - ? null : context.getResources().getDrawable(idPressed);
Drawable focus = idFocused == - ? null : context.getResources().getDrawable(idFocused);
//注意该处的顺序,只要有一个状态与之相配,背景就会被换掉
//所以不要把大范围放在前面了,如果sd.addState(new[]{},normal)放在第一个的话,就没有什么效果了
sd.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focus);
sd.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
sd.addState(new int[]{android.R.attr.state_focused}, focus);
sd.addState(new int[]{android.R.attr.state_pressed}, pressed);
sd.addState(new int[]{android.R.attr.state_enabled}, normal);
sd.addState(new int[]{}, normal);
return sd;
}

xml方式实现时,可以先了解下对应xml的语法

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

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

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

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

android:variablePadding=["true" | "false"] >

<item

android:drawable="@[package:]drawable/drawable_resource"

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>

下面对应的具体实例,由于是做背景用,该xml将放于/res/drawable下面(StateList中第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,这也是为什么默认的值必须总是在最后)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/btn_selected"/>
<item android:state_focused="true" android:drawable="@drawable/btn_selected"/>
<item android:state_enabled="true" android:drawable="@drawable/btn_normal"/>
<item android:drawable="@drawable/btn_normal" />
</selector>

在Button的xml中进行加载:

 <Button
android:id="@+id/canel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_cancel"
android:layout_margin="10dip"
android:layout_weight=""
android:textColor="#ffffffff"
android:textSize="15sp"
android:background="@drawable/button_drawable"
/>

或在java代码中加载:

okBtn.setBackgroundDrawable(R.drawable.button_drawable);  

最新文章

  1. Android-java基础内部类
  2. 深入分析ClassLoader
  3. 利用ngxtop实时监控nginx的访问情况
  4. make -e install ,,,make命令的-e选项!
  5. Python中的Class的讨论
  6. 腾讯大讲堂ppt全集
  7. php 文本框里面显示数据库调出来的资料
  8. modifytime是一个神奇的column name----这边文章是错的totally,因为我的实验不彻底。timestamp属性很神奇,头一个timestamp,会自动的成DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
  9. CSS入门学习(转)
  10. 转载:mysql-Auto_increment值修改
  11. JSTL解析——004——core标签库03
  12. 因下面文的损坏或丢失windows/system32/config/system 解决方法
  13. POJ 半平面交 模板题 三枚
  14. get请求与post请求之间的差异
  15. poj3067树状数组求逆序数
  16. AngularJS -- Module (模块)
  17. hdu4966 GGS-DDU
  18. 真正让HTML标签、DIV、SPAN拥有onfocus和onblur,聚焦和失焦
  19. 6.MongoDB4.0在Windows环境的下载、安装、配置
  20. Playmaker全面实践教程之Playmaker常用工具

热门文章

  1. activiti框架 数据库设计说明
  2. Android NOTE
  3. RedMine 版本管理工具
  4. Oracle EBS-SQL (BOM-6):检查物料失效但BOM中未失效的数据.sql
  5. [推荐] 查看网站使用的JS框架
  6. jQuery-zclip实现复制内容到剪切板
  7. Android之Service与IntentService的比较
  8. 杭电 1272 POJ 1308 小希的迷宫
  9. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(八)
  10. 美国TJX公司 - MBA智库百科