pcatzj
V2EX  ›  Android

Android selector 疑问

  •  
  •   pcatzj · Nov 9, 2016 · 12298 views
    This topic created in 3498 days ago, the information mentioned may be changed or developed.

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

    9 replies    2016-11-11 09:47:46 +08:00
    jimbray
        1
    jimbray  
       Nov 9, 2016
    有一个取巧的办法 color.xml 新建一个 #00000000 的 drawable
    selector 里面直接 设置 @drawable/xxxx 即可
    pcatzj
        2
    pcatzj  
    OP
       Nov 9, 2016
    @jimbray 好的,感谢,我试试
    pcatzj
        3
    pcatzj  
    OP
       Nov 9, 2016
    不行,这样的话选中状态的效果也不生效了
    pcatzj
        4
    pcatzj  
    OP
       Nov 9, 2016
    @jimbray 不行,这样的话选中状态的效果也不生效了
    bqbkbz
        5
    bqbkbz  
       Nov 9, 2016
    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  
       Nov 10, 2016
    @pcatzj 楼上说的对。
    pcatzj
        7
    pcatzj  
    OP
       Nov 10, 2016
    @zhaohui318
    @jimbray
    是放在最后的,我一般习惯都是把最后默认的写在最后,但是不管是设置 @null 还是#00000000 还是不写默认的都不行
    bqbkbz
        8
    bqbkbz  
       Nov 10, 2016
    检查一下你的代码是不是哪里没写对
    我试了下 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
       Nov 11, 2016
    @zhaohui318 不写的效果是什么呢,我试的时候是选中时的效果也没有了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3277 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 12:15 · PVG 20:15 · LAX 05:15 · JFK 08:15
    ♥ Do have faith in what you're doing.