Android,setSelected() 和 state_selected
我在使用 View.setSelected()
时遇到问题。 Views
被标记为已选择 - 例如,TextViews
更改其字体颜色 - 但我的背景选择器似乎没有注册更改。
示例选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/transparent" />
<item android:state_selected="true">
<shape android:shape="rectangle">
<solid android:color="#ff8600" />
</shape>
</item>
</selector>
我什至不确定哪种上下文信息有用。这些视图是 LinearLayout 的子级,我以编程方式在触摸事件内设置选定状态。正如我所说,它似乎确实有效,因为字体颜色从白色变为灰色,但背景保持不变。
编辑:我在发布之前检查了是否有愚蠢的错误:P。答案不是“添加 android:background 属性”。
I'm having trouble with View.setSelected()
. Views
are flagged as selected -- TextViews
, for example, change their font color -- but my background selectors don't seem to register the change.
Example selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/transparent" />
<item android:state_selected="true">
<shape android:shape="rectangle">
<solid android:color="#ff8600" />
</shape>
</item>
</selector>
I'm not even sure which kind of context information would be useful. The views are children of a LinearLayout, and I'm programatically setting the selected state inside a touch event. As I said, it does seem to work, since the font color goes from white to gray, but the background stays the same.
Edit: I checked for silly mistakes before posting :P. The answer is not "add the android:background attribute".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
项目的顺序在选择器 xml 中很重要,默认项目应始终位于项目列表的底部。
The order of items matters in selector xmls, the default item should always be at the bottom in the items list.
不仅所选状态的顺序很重要,甚至所有状态的顺序也很重要。就我而言,我首先添加了
state_pressed
,但我的state_selected
不起作用。所以我像这样改变了顺序,然后它起作用了:编辑
我现在也面临这个问题,如果我按下按钮,它将处于选定状态,但不会处于按下状态。因此,解决方案必须是,要对这样的状态和附加状态进行排序,添加默认按钮外观是一个很好的做法:
首先,设置选择状态,然后交替设置与按下状态相同的状态。 (目前,stackoverflow 没有完全显示我的编辑,不知道为什么,请耐心等待)。
not only the selected state order is important, even the order of all states is important. In my case, I added
state_pressed
as first and mystate_selected
doesn´t work. So I changed the order like this, and then it worked:EDIT
I faced now also the problem, if I press the button, it will be in the selected-state but not in pressed-state. So, the solution must be, to order the states like this and additional, it´s a good practise to add a default button look:
First, set the selected state and after this, set the same as pressed state alternately. (For now, stackoverflow isn´t showing my edit completely, don´t know why, just be patient please).
原因并不明显,但我认为这可能有效(注意添加了 state_selected="false"):
我希望这有帮助。
It's not obvious why, but I think this might work (notice the addition of state_selected="false"):
I hope that's helpful.