如何更改Android按钮单击颜色

时间:2013-10-29 06:51:44

标签: android button background

我有一个按钮。我已使用background属性将背景图像添加到我的按钮。现在当我点击按钮时,我没有得到默认的橙色。我怎样才能得到那种颜色。

还有一个问题,在上面的场景中,如何将默认的橙色更改为其他颜色。

5 个答案:

答案 0 :(得分:3)

答案 1 :(得分:2)

您必须自定义按钮以进行更改,以便在yoyr drawable文件夹中创建名为custom_btn的xml文件,并将代码粘贴到其中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_checked="true"
           android:drawable="@drawable/tick_btn" /> <!-- pressed -->
        <item android:drawable="@drawable/untick_btn" /> <!-- default -->
 </selector>

并在按钮中添加android:button="@drawable/custom_btn"

答案 2 :(得分:0)

// try this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="hover color or drawable" android:state_pressed="true"></item>
    <item android:drawable="hover color or drawable" android:state_focused="true"></item>
    <item android:drawable="color or drawable" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
    <item android:drawable="hover color or drawable" android:state_enabled="false"></item>

</selector>

答案 3 :(得分:0)

如果你只是想看到“默认的橙色”,那么使用ImageButton并将你的drawable应用到src(而不是背景)。然后,您将看到图像背后的原生图像背景,它将继续执行之前所做的任何事情。

答案 4 :(得分:0)

我没有获得默认的橙色。我怎样才能获得这种颜色。

  

当您向按钮添加背景时,它不再是默认的原始按钮,它变为自定义按钮,因为您通过向其添加一些背景来呈现默认行为。现在,您需要自己为自定义按钮添加颜色,因为操作系统仅处理非自定义的原始按钮。

如何将默认橙色更改为其他颜色。

按下按钮后可以用两种方式更改按钮状态

1)添加背景图片。

2)或在按钮上添加xml。

这是关于Customizing Android buttons的非常好的教程。