pcatzj
V2EX  ›  Android

Android selector 疑问

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