想请问一下, Android 的 selector 能不能设置点击时是一种背景,但是不点击时不设置背景?如果可以,那应该怎么设置?我在 selector 里面直接设置的 @null 报错。
1
jimbray 2016-11-09 14:04:58 +08:00
有一个取巧的办法 color.xml 新建一个 #00000000 的 drawable
selector 里面直接 设置 @drawable/xxxx 即可 |
3
pcatzj OP 不行,这样的话选中状态的效果也不生效了
|
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 要放在最后面 |
7
pcatzj OP |
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" /> |
9
pcatzj OP @zhaohui318 不写的效果是什么呢,我试的时候是选中时的效果也没有了
|