如何设置禁用按钮灰色或阴影?

时间:2013-02-27 11:31:05

标签: android

我有一个包含具有图像背景的按钮的xml。其中一些是禁用的,但是当我在其上放置图像背景时,禁用的按钮将不会设置为灰色背景。

2 个答案:

答案 0 :(得分:0)

创建一个可绘制的选择器背景,为不同的状态提供不同的图像,

答案 1 :(得分:0)

你需要的是selector标签。你甚至不需要关心编程onFocus()等等。它非常容易和简单。你需要构建一次xml布局,它将完成剩下的工作。

将可绘制按钮背景添加到drawable文件夹后,您需要添加一个 像这样的选择器:

(适用RES /抽拉/ 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>

现在只需将此drawable设置为您的按钮背景,如:

 <Button
        android:id="@+id/imageButtonSelector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/new_button" />

你的作品完成了。 Here是带有示例项目源代码的教程,供您遵循。