我有一个按钮,我将其背景设置为特定selector
.
选择器当前更改按钮背景并更改图像作为背景。
我还希望更改背景颜色(图像是周围有透明空间的图标)。
这是选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- default -->
<item
android:state_pressed="false"
android:state_focused="false"
android:drawable="@drawable/menu_button_collapsed" >
</item>
<!-- button focused -->
<item
android:state_pressed="false"
android:state_focused="true"
android:drawable="@drawable/menu_button_collapsed_highlight"
android:drawable="@drawable/button_background" >
</item>
<!-- button pressed -->
<item
android:state_pressed="true"
android:state_focused="false"
android:drawable="@drawable/menu_button_collapsed_highlight"
android:drawable="@drawable/button_background" >
</item>
</selector>
正如你所看到的,我设置了drawable
属性两次,这是非法的,但这正是我想要的。
Notice @drawable/button_background
只是一种颜色
创建一个新的<layer-list>
可绘制的
自定义按钮.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Your background color goes first -->
<item
android:id="@android:id/background"
android:drawable="@drawable/button_background" />
<!-- Your button icon image -->
<item
android:id="@android:id/button_image"
android:drawable="@drawable/menu_button_collapsed_highlight" />
</layer-list>
并在您的选择器可绘制文件中引用它
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- default -->
<item
android:state_pressed="false"
android:state_focused="false"
android:drawable="@drawable/menu_button_collapsed"
/>
<!-- button focused -->
<item
android:state_pressed="false"
android:state_focused="true"
android:drawable="@drawable/custom_button"
/>
<!-- button pressed -->
<item
android:state_pressed="true"
android:state_focused="false"
android:drawable="@drawable/custom_button"
/>
</selector>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)