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. CSS3+HTML5特效5 - 震动的文字

    先看效果(把鼠标移上去看看) abcd 这个效果很简单,就是移动文字的位置模拟出震动的效果. Css <style> @-webkit-keyframes shake { 0%{ -web ...

  2. [Attila GPU] ATTILA GPU Streamer Unit (D3D Input Assambler) 结构分析

    http://www.opengpu.org/forum.php?mod=viewthread&tid=40&highlight=Attila 查看: 7488|回复: 26    [ ...

  3. JavaScript随记汇总

    1.<script>标签嵌套,浏览器无法正常解析的问题: 百度知道回答 <script>FTAPI_slotid = 1007894;FTAPI_sync = true< ...

  4. qml能够这么玩

    Qt 5以后qmlscene被qml所替代,/usr/bin/qml能够用来执行.qml文件.所以,我们就能够和sh一样的来写界面了. #!/usr/bin/env qml import QtQuic ...

  5. 原生AJAX基础讲解及兼容处理

    原文:原生AJAX基础讲解及兼容处理 AJAX = Asynchronous JavaScript and XML (异步的JavaScript和XML). AJAX不是新技术 ,但却是热门的技术.它 ...

  6. 增加VMWare开机画面时间,来防止快速跳过而无法进入BIOS

    用记事本打开xx.vmx,在里面添加一行: bios.bootDelay = "30000" 意思是开机后,在开机画面里停留30秒.

  7. Session变量不能转移到下页.解决: session.use_trans_sid = 1

    附:文摘 ============================================================ 在PHP使用SESSION的朋友可能会碰到这么一个问题.SESSIO ...

  8. WinForm中回车键实现文本框之间的跳转

    利用窗体的KeyPreView .设置KeyPreView = true 设置窗体的KeyPreView 属性为True后,那么窗体内的子控件响应KeyPress事件(或其他事件)之前,会先响应窗体的 ...

  9. Qt的SQL操作,DELETE,SELECT

    #include <QObject> #include <QSqlDatabase> #include <QSqlError> #include <QSqlQ ...

  10. 史上最全的Matlab资源电子书教程和视频下载合集【超级推荐】

    收藏吧,网上搜集的,费了老大劲了,推荐给有需要的人,^_^.   MATLAB课件2007北京交通大学.zip 4.87 MB   A Guide to MATLAB for Beginners an ...