我的应用中有一些透明背景的按钮。现在,我想定制它们来保持透明背景,但是当它们被按下时,背景应该变成绿色。将按钮背景从透明变为彩色时按下
我知道这里有很多关于自定义按钮的主题,我收到了其中几个主题,还有很多来自谷歌的教程。虽然这看起来很容易,但我没有得到它的工作。
这是我的按钮的示例代码:
<Button
android:id="@+id/accept_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/button_state"
android:text="@string/btnaccept"
android:textStyle="bold" />
这是选择XML文件buton_state.xml
在那里我已经定义了diferent按钮状态的背景颜色变化:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button focused and pressed-->
<item android:state_pressed="true"
android:state_focused="true" >
<shape>
<solid
android:color="@color/LightGreen" />
</shape>
</item>
<!-- Button Default-->
<item android:state_pressed="false"
android:state_focused="false" >
<shape>
<solid
android:color="@android:color/transparent" />
</shape>
</item>
</selector>
这文件在res/drawable文件夹中。
在我的应用程序中,按钮可以正确地获得默认的透明背景,但是在聚焦或按下时,此背景颜色不会变为绿色。
我不知道为什么,但我尝试了类似的方式,并没有工作......但现在它工作。做得好! – masmic