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. my97 日期控件

    官网:http://www.my97.net/   好多广告啊! 文档地址: http://www.mysuc.com/test/My97DatePicker/

  2. 从WebBrowser中取得Cookie 和 WebClient设置cookie!

    原文:从WebBrowser中取得Cookie 和 WebClient设置cookie! 从WebBrowser中取得Cookie 的代码 CookieContainer myCookieContai ...

  3. 直读Innodb datafile

    这两天有空翻了翻大神写的<innodb存储引擎>,手痒亲身实践.由于此书出版了有段时日,没有用其推荐的python工具,通过点滴推敲,略微发现其中冰山一角的奥秘.对于今后对于一些问题查证或 ...

  4. 工作笔记3.手把手教你搭建SSH(struts2+hibernate+spring)环境

    上文中我们介绍<工作笔记2.软件开发经常使用工具> 从今天開始本文将教大家怎样进行开发?本文以搭建SSH(struts2+hibernate+spring)框架为例,共分为3步: 1)3个 ...

  5. Excel 宏

    实现1到40行的第一列 ,全部 累加一个字符串 A1 Sub Macro1() Dim i As IntegerFor i = 1 To 40Sheets(1).Cells(i, 1).Value = ...

  6. 10个实用的PHP正则表达式汇总

    原文 10个实用的PHP正则表达式汇总 正则表达式是程序开发中一个重要的元素,它提供用来描述或匹配文本的字符串,如特定的字符.词或算式等.但在某些情况下,用正则表达式去验证一个字符串比较复杂和费时.本 ...

  7. PHP 4:从Login进一步看到的

    原文:PHP 4:从Login进一步看到的 我们已经在PHP 3:从Login界面谈PHP标记谈到了PHP标记,不过其页面代码有一句 require_once('bookmark_fns.php'); ...

  8. hdu2899 Strange fuction

    在区间(0,100).在恒大二阶导数0.f(x)有极小值.用的最低要求的一阶导数值点: #include<math.h> #include<stdio.h> #include& ...

  9. 使用八种牛云存储解决方案ios7.1的app部署问题

    使用八种牛云存储解决方案ios7.1的app部署问题 一个.问题叙述性说明 开发完ios版本号的app.须要将.ipa文件和.plist文件打包上传,供用户下载,在线安装.用户安装过程简单描写叙述例如 ...

  10. OSG(OpenSceneGraphcow.osg)配置笔记

    OpenSceneGraph是一款高性能的3D图形开发库.广泛应用在可视化仿真.游戏.虚拟现实.高端技术研发以及建模等领域.使用标准的C++和OpenGL编写而成,可以运行在Windows系列.OSX ...