我有一个 xml,其中包含具有图像背景的按钮。其中一些是禁用的,但是当我在其上放置图像背景时,禁用的按钮将不会设置为灰色背景。
问问题
1332 次
2 回答
0
创建一个可绘制的选择器背景,为不同的状态提供不同的图像,
于 2013-02-27T11:33:18.147 回答
0
What you need are selector
tags.You dont even need to care about programming onFocus()
etc.Its very easy and simple.You need to construct an xml layout once and it will do the rest.
after adding your drawable button backgrounds to the drawable folder you need to add a selector like this one:
(res/drawable/new_button.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed_yellow"
android:state_pressed="true" />
<item android:drawable="@drawable/button_focused_orange"
android:state_focused="true" />
<item android:drawable="@drawable/button_normal_green" />
</selector>
Now just set this drawable to your button background like:
<Button
android:id="@+id/imageButtonSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/new_button" />
Your works done. Here is tutorial with sample project source code for you to follow.
于 2013-02-27T11:44:34.603 回答