V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
pcatzj
V2EX  ›  Android

Android selector 疑问

  •  
  •   pcatzj · 2016-11-09 13:36:15 +08:00 · 4307 次点击
    这是一个创建于 2726 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想请问一下, Android 的 selector 能不能设置点击时是一种背景,但是不点击时不设置背景?如果可以,那应该怎么设置?我在 selector 里面直接设置的 @null 报错。

    9 条回复    2016-11-11 09:47:46 +08:00
    jimbray
        1
    jimbray  
       2016-11-09 14:04:58 +08:00
    有一个取巧的办法 color.xml 新建一个 #00000000 的 drawable
    selector 里面直接 设置 @drawable/xxxx 即可
    pcatzj
        2
    pcatzj  
    OP
       2016-11-09 14:07:47 +08:00
    @jimbray 好的,感谢,我试试
    pcatzj
        3
    pcatzj  
    OP
       2016-11-09 14:15:53 +08:00
    不行,这样的话选中状态的效果也不生效了
    pcatzj
        4
    pcatzj  
    OP
       2016-11-09 14:16:10 +08:00
    @jimbray 不行,这样的话选中状态的效果也不生效了
    zhaohui318
        5
    zhaohui318  
       2016-11-09 18:50:11 +08:00
    Note: Remember that the first item in the state list that matches the current state of the object will be applied. So if the first item in the list contains none of the state attributes above, then it will be applied every time, which is why your default value should always be last, as demonstrated in the following example.

    https://developer.android.com/guide/topics/resources/color-list-resource.html

    default 要放在最后面
    jimbray
        6
    jimbray  
       2016-11-10 08:58:43 +08:00
    @pcatzj 楼上说的对。
    pcatzj
        7
    pcatzj  
    OP
       2016-11-10 16:13:52 +08:00
    @zhaohui318
    @jimbray
    是放在最后的,我一般习惯都是把最后默认的写在最后,但是不管是设置 @null 还是#00000000 还是不写默认的都不行
    zhaohui318
        8
    zhaohui318  
       2016-11-10 19:00:11 +08:00
    检查一下你的代码是不是哪里没写对
    我试了下 default 的可以不写

    EXAMPLE:

    XML file saved at res/drawable/button.xml:

    <?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/button_pressed" /> <!-- pressed -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
    </selector>

    This layout XML applies the state list drawable to a Button:

    <Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Hello World!"
    android:background="@drawable/button" />
    pcatzj
        9
    pcatzj  
    OP
       2016-11-11 09:47:46 +08:00
    @zhaohui318 不写的效果是什么呢,我试的时候是选中时的效果也没有了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2807 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 06:15 · PVG 14:15 · LAX 23:15 · JFK 02:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.