2014-03-12 38 views
-1

任何人都可以帮助我更改我的按钮的背景颜色,以便点击后可以更改颜色,点击后可以更改为正常颜色。我对使用图像并不感兴趣,并且看过,每个人都保留一半的代码,所以我仍然不完全理解。如果可能的话,请提供一个链接,我可以看到一步一步的指示,这些张贴评论并没有帮助我,因为我完全不熟悉Android和Java。点击后的Android按钮背景颜色

+0

[点击时更改按钮颜色的问题]的可能重复(http://stackoverflow.com/questions/7301029/question-about-change-the-colours-of-a-button-when-clicked ) – 323go

回答

1

你需要为选择器元素做特殊的drawable。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
      android:drawable="@drawable/button_pressed" /> <!-- pressed --> 
    <item android:state_focused="true" 
      android:drawable="@drawable/button_focused" /> <!-- focused --> 
    <item android:state_hovered="true" 
      android:drawable="@drawable/button_focused" /> <!-- hovered --> 
    <item android:drawable="@drawable/button_normal" /> <!-- default --> 
</selector> 

,并在按钮

<Button 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:background="@drawable/button" /> 

更多信息here

0

<item 
    android:state_window_focused="false" 
    android:drawable="@android:color/transparent" /> 
<item 
    android:state_pressed="true" 
    android:drawable="@drawable/item selected"/> 
<item 
    android:state_focused="true" 
    android:drawable="@drawable/item selected"/> 
<item 
    android:drawable="@android:color/transparent"/> 

将这个XML以可抽拉FIL e并设置资源。

+0

谢谢你试图帮忙,但就像我说过的,参考和例子文件名以及放在什么地方。实际的android网站很混乱,不适合初学者。一个实际的例子是我需要的。项目在哪个文件中出现,以及我需要在哪些地方引用此内容在主要activity.java。你看这对我来说毫无意义,因为我正在学习,而且我不能让这件事情起作用。 – user3365065

相关问题