![](/img/trans.png)
[英]Android - How to create image button of different states, default , pressed and focussed
[英]Android three button states (pressed, not pressed and default)
加载布局时,所有按钮将具有默认状态/ bg(选择器中没有状态)。 当按下按钮时,该按钮的背景将更改为button_pressed =“ true”,所有其他按钮的背景也会更改为button_pressed =“ false”。 可能吗?
使用此代码,默认背景为button_pressed =“ false”项目。
<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item
android:state_pressed="true"
android:drawable="@drawable/buttonClicked" />
<item
android:state_pressed="false"
android:drawable="@drawable/buttonNotClicked" />
<item
android:drawable="@drawable/button" />
</selector>
您的button_selector
应该如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/button_focused" android:state_focused="true"/>
<item android:drawable="@drawable/button_normal"/>
</selector>
在这里,您可以具有3种状态,即button_normal
(当按钮处于正常状态时), button_focused
(当按钮被聚焦以按下时)和button_pressed
(在按下按钮之后)。
因此,使用以下名称的4个不同的drawables(在/res/drawable
文件夹中):
button_selector.xml
(用于选择器) button_normal.xml
(普通按钮) button_focused.xml
(聚焦时的按钮) button_pressed.xml
(按下按钮时) 希望能帮助到你! 让我知道我是否可以帮助您...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.