state_checkable 不适用于扩展 checkable 的视图
我有一个可绘制状态列表选择器,需要在选择时显示不同的可绘制对象。
我扩展了使用带有接口 Checkable
的可绘制对象的 View 类,并且工作正常。
但是,当我仅过滤 state_checkable
时,它会显示 false 值的图像。我认为这意味着我的实现有问题。
<item android:drawable="@drawable/button_6_default" android:state_checkable="false" />
<item android:drawable="@drawable/button_disable_default" android:state_checkable="true" />
如何让 state_checkable
和 state_checked
正常工作?
I have a drawable state list selector that needs to display a different drawable when selected.
I extended the View class that uses the drawable with interface Checkable
and this works ok.
However, when I only filter on state_checkable
, it displays the image for the value of false. I assume this means that there is something wrong with my implementation.
<item android:drawable="@drawable/button_6_default" android:state_checkable="false" />
<item android:drawable="@drawable/button_disable_default" android:state_checkable="true" />
How do you get state_checkable
and state_checked
to work properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为可能需要 onCreateDrawableState 方法的实际代码的其他人扩展之前的答案。这是从这个库中提取的: https://github.com/ManuelPeinado/MultiChoiceAdapter
Extending the previous answer for anyone else who may need the actual code for the onCreateDrawableState method. This is extracted from this library: https://github.com/ManuelPeinado/MultiChoiceAdapter
我想通了。如果在调用 super 后检查视图并增加您添加的额外属性的数量,您需要实现 onCreateDrawableState 并添加 state_checked 属性。抱歉,这里不再有代码了。
I figured it out. You need to implement
onCreateDrawableState
and add the state_checked attr if the view is checked after calling super with an increased count of the number of extra attributes you have added. Don't have the code right here anymore, sorry.