1.下面是不生效的布局:

  • selector_btn_red.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
</item>
<item android:state_pressed="true" android:drawable="@drawable/btn_red_pressed"></item>
<item android:state_pressed="false" android:drawable="@drawable/btn_red_default"></item>
<item android:state_enabled="false" android:drawable="@drawable/btn_red_default_disable">
</selector>
  • xxx_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myattr="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/download_item_padding" > <Button
android:id="@+id/remove_btn"
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_full_width_btn_height"
android:layout_gravity="bottom"
android:background="@drawable/selector_btn_red"
android:enabled="false"
android:text="@string/remove"
android:visibility="visible" />
</LinearLayout> </LinearLayout>out>

2. 解析:按照初始的想法,这样的布局应当显示状态为enable=false的selector中指定的图(灰色)。但是事实不是这样的他显示了第一个(亮红)。为什么出现这种情况呢?这是因为android在解析selector时是从上往下找的,找到一个匹配的即返回。而我们第一个item中并没有指定enable,android认为满足条件返回了。

3.解决方法有两种:

  1). 交换item位置:

  

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

  2). 为每个item设置enable值:

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

以上两种方案择其一种即可。

Android开发问题集锦-Button初始为disable状态时自定义的selector不生效问题的更多相关文章

  1. Android开发中如何解决加载大图片时内存溢出的问题

    Android开发中如何解决加载大图片时内存溢出的问题    在Android开发过程中,我们经常会遇到加载的图片过大导致内存溢出的问题,其实类似这样的问题已经屡见不鲜了,下面将一些好的解决方案分享给 ...

  2. Android开发系列之button事件的4种写法

    经过前两篇blog的铺垫,我们今天热身一下,做个简单的样例. 文件夹结构还是引用上篇blog的截图. 详细实现代码: public class MainActivity extends Activit ...

  3. Android开发 更改返回button的图标

    非常多的Android应用左上角都有返回button 在默认的情况下 ADT会默认给一个返回图标 而作为开发需求 非常多都要求定制一个新的图标 在Android的站点上 发现了2种能够更改的方法 1. ...

  4. 【Android 应用开发】Android 开发错误集锦

    1. eclipse的Device中不显示手机 在eclipse中连接不上手机,出现adb server didn't ACK  fail to start daemon 错误. 出现这种原因是因为a ...

  5. Android开发FAQ集锦!!!

    .Android SDK应该从什么地方下载?为什么(http://developer.Android.com/ )经常上不上去? 答:谷歌官网的 (http://developer.Android.c ...

  6. Android 开发错误集锦

    1. eclipse的Device中不显示手机 在eclipse中连接不上手机,出现adb server didn't ACK  fail to start daemon 错误. 出现这种原因是因为a ...

  7. Android开发如何在4.0及以上系统中自定义TitleBar

    本文将通过一个实例讲解怎么实现在4.0及以上系统版本中实现自定义TitleBar,这只是我自己找到的一种方法; xml布局文件 activity_main.xml <RelativeLayout ...

  8. Android中ListView中有button,checkbox,GridView时事件问题

    最近做项目,用到了listview的item的一些问题,现在抽空把它们总结一下: 转载请表明出处:http://blog.csdn.net/wdaming1986/article/details/67 ...

  9. Android开发教程 - 使用Data Binding(八)使用自定义Interface

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

随机推荐

  1. bootstrap collapse MVC .net漂亮的折叠List

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...

  2. 自动生成Makefile文件

    主要的工具有autoscan, aclocal, autoheader, autoconfig,automake 1 .创建c源文件hello.c #include <stdio.h> i ...

  3. s性能优化方面的小知识

    总结的js性能优化方面的小知识 前言 一直在学习javascript,也有看过<犀利开发Jquery内核详解与实践>,对这本书的评价只有两个字犀利,可能是对javascript理解的还不够 ...

  4. 动态创建一些常的html标签

    原文:动态创建一些常的html标签 一段时间来,不管是在学习还是应用asp.net mvc应用程序,较多情况之下,需要动态创建一些html标签.如这篇<文本框下面有两个铵钮,点就加点减就减> ...

  5. 使用AndroidStudio快速开发教程

    关于AndroidStudio的使用 参考:http://www.codes51.com/article/detail_98914.html  1.对于开发环境的通性:编写 调试 视图   一般的开发 ...

  6. 深入理解JavaScript(1)

    才华横溢的Stoyan Stefanov,在他写的由O’Reilly初版的新书<JavaScript Patterns>(JavaScript模式)中,我想要是为我们的读者贡献其摘要,那会 ...

  7. C#修改用户名

    string strCmdText; strCmdText = "useraccount where name='" + 旧密码 + "' rename " + ...

  8. javascript实现代码高亮

    javascript实现代码高亮-wangHighLighter.js 1. 引言 (先贴出wangHighLighter.js的github地址:https://github.com/wangfup ...

  9. .NET程序保护专家.NET Reactor发布4.7版本

    .NET Reactor是一款功能强大的代码保护以及许可授权管理系统. 关于代码混淆,针对.NET程序程序而言,.NET Reactor保护的程序目前还没有被破解过.这与.NET Reactor的保护 ...

  10. leetcode第40题--First Missing Positive

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...