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. SSAS系列——【05】多维数据(编程体系结构)

    原文:SSAS系列--[05]多维数据(编程体系结构) 1.什么是AMO? 翻译:AMO是SSAS中一个完整的管理类集合,它在Microsoft.AnalysisServices命名空间下,我们可以在 ...

  2. Unix/Linux环境C编程新手教程(41) C语言库函数的文件操作具体解释

     上一篇博客我们解说了怎样使用Linux提供的文件操作函数,本文主要解说使用C语言提供的文件操作的库函数. 1.函数介绍 fopen(打开文件) 相关函数 open,fclose 表头文件 #in ...

  3. Unofficial Microsoft SQL Server Driver for PHP (sqlsrv)非官方的PHP SQL Server 驱动

    原文 Unofficial Microsoft SQL Server Driver for PHP (sqlsrv) Here are unofficial modified builds of Mi ...

  4. 如何去除configure的默认选择-g O2

    http://lists.gnu.org/archive/html/autoconf/2006-04/msg00002.html http://www.linuxidc.com/Linux/2013- ...

  5. [LeetCode] ZigZag Conversion [9]

    称号 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  6. 安装Oracle 9i - 初学者系列 - 学习者系列文章

    Oracle 9i数据库是经典的Oracle版本,就象SQL Server 2000一样.笔者最初使用到的Oracle版本就是Oracle 9i.下面就介绍下Oracle 9i的安装. 1.  下载O ...

  7. SQL Server 数据库定时自动备份

    原文:SQL Server 数据库定时自动备份 SQL Server 数据库定时自动备份——每天定时备份,保留前8天的备份 利用SQL Server代理新建作业来定期备份 1)在数据库库的[SQL S ...

  8. !DOCTYPE html文档类型声明简写 HTML5 DOCTYPE缩写

    html5之!DOCTYPE html文档类型声明简写,在HTML5中DOCTYPE简写非常重要. 一.概述   -   TOP 让CSS样式表生效,DOCTYPE声明是必须的,以前TABLE布局的网 ...

  9. HTML 速成

    html零基础者入. 记得学计算机网络的时候好像有学过一些HTML,但没运用起来都忘光了.近来想学学如何写网页.就从html(HyperText Markup Language超文本标记语言)入手了. ...

  10. 【转】GitHub 优秀的 Android 开源项目

    转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介绍那些不错个性化的View,包括ListView.ActionBar.M ...