单击图像按钮时 Selector.xml 不起作用
当我单击它时,我使用以下选择器来更改图像按钮的图像。但这在大多数情况下不起作用。仅适用于少数按钮。不知道这里的原因是什么
mapselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button Pressed -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/mapiconover"/>
</selector>
,在按钮中,
我这样做,
<ImageButton
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/btnMap" android:layout_centerInParent="true" android:src="@drawable/mapselector"
android:background="@drawable/map" android:scaleType="fitXY"
android:layout_alignParentBottom="true">
</ImageButton>
但当我单击时,静止图像没有改变。
对此有什么猜测吗?
I use the following selector to change image of image button, when I click on it. But this does not work most of the times. Only for few buttons it works. Don't know what the cause here is
mapselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button Pressed -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/mapiconover"/>
</selector>
And in the button,
Am doing like this,
<ImageButton
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/btnMap" android:layout_centerInParent="true" android:src="@drawable/mapselector"
android:background="@drawable/map" android:scaleType="fitXY"
android:layout_alignParentBottom="true">
</ImageButton>
but still image is not changing when I click.
Any guess for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已经在 ImageButton xml 中创建了这样的内容,
使用按钮图像创建 xml 文件,如下所示
将该 xml 文件添加为 IMageButton 的背景
you have create like this in the ImageButton xml
create xml file using the button image like this
add that xml file as background for IMageButton
不确定这是否有效,但您可以尝试将选择器更改为:
将图像按钮更改为:
Not sure if this will work but you could try changing your selector to this:
And your image button to this:
我也有同样的问题。对我来说是因为
冲突。它通过澄清启用和按下之间的条件来解决。我使用以下代码来获取按下状态和正常状态之间的不同状态:
此示例对不同状态使用不同的渐变和笔划。您也可以使用您的可绘制对象来代替。只需在您的可绘制文件夹中创建一个类似“mybutton.xml”的文件即可。然后像这样设置按钮的背景属性:
我希望它有帮助。
I had the same problem. For me it was because of
conflict. It solved by clarifying the conditions between enabled and pressed. I used the following code to get different states between press and normal state:
This example uses different gradient and stroke for different states. You can also use your drawable instead. Just create a file like "mybutton.xml" in your drawable folder. then set the background property of your button like this:
I hope it be helpful.