2017-03-06 248 views
-1

当我点击按钮时,我已经尝试了一个小时以使button的颜色发生变化。当按下按钮时按钮颜色发生变化

这里是我的 “mybutton.xml”

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

    android:shape="rectangle"> 
    <gradient 
     android:startColor="#c4161616" 
     android:endColor="#c4161616" 
     android:angle="270" /> 
    <corners 
     android:radius="10dp" /> 
    <stroke 
     android:width="5px" 
     android:color="#00FF00" /> 
</shape> 

现在按钮看起来是这样的:

enter image description here

而当按钮被按下它应该是这样的:

enter image description here

<gradient 
    android:startColor="#c4161616" 
    android:endColor="#c4565656" 
    android:angle="270" /> 

我是Android和XML的初学者。你能告诉我怎么做吗?

+0

你需要选择这样做。定义onfocus状态,然后将其作为按钮的背景。它会像魅力一样工作。祝你好运 – koksalb

回答

0

兄弟试试这个: -

<?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>