2017-03-06 331 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>