2015-02-11 95 views
0

我想在用戶點擊一個按鈕時使用2張圖片來產生效果。 我認爲我做得很好,但效果不起作用。Android按鈕效果不起作用

具體來說,按鈕背景設置爲camera_button_after.jpg,它的意思是按下狀態,點擊時不會改變。

你能告訴我什麼是錯?我會非常感激。

這裏是包含按鈕的main_frame.xml。

main_btn1.xml,main_btn2.xml,main_btn3.xml是選擇器。 這三個選擇具有相同的結構,也同樣的問題..

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<Button 
    android:id="@+id/button001" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:background="@drawable/main_btn1" 
    android:text=" Mood" 
    android:textSize="30dp"/> 

<Button 
    android:id="@+id/button002" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:background="@drawable/main_btn2" 
    android:text=" Mood Lists" 
    android:textSize="30dp"/> 

<Button 
    android:id="@+id/button003" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:background="@drawable/main_btn3" 
    android:text=" Help" 
    android:textSize="30dp" /> 

</LinearLayout> 

這裏是main_btn1.xml

<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item 
     android:state_pressed="false" 
     android:drawable="@drawable/camera_button_before" /> 

    <item 
     android:state_pressed="true" 
     android:drawable="@drawable/camera_button_after"/> 

</selector> 

一個問題is..I認爲Eclipse不充分認識到圖片和xml文件。我打掃項目幾次,然後重新啓動Eclipse的,但他們仍然有問號像下面

enter image description here

回答

0

我正面臨類似的問題。我嘗試了下面的代碼。我確實從堆棧溢出本身獲得了此解決方案,但沒有鏈接供參考。因此,在可繪製文件夾中編寫xml的代碼片段。請記住提供正確的圖像名稱:)

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
    android:state_enabled="false" 
    android:drawable="@drawable/image1" /> 
<item 
    android:state_pressed="true" 
    android:state_enabled="true" 
    android:drawable="@drawable/image2" /> 
<item 
    android:state_focused="true" 
    android:state_enabled="true" 
    android:drawable="@drawable/image3" /> 
<item 
    android:state_enabled="true" 
    android:drawable="@drawable/image4" /> 
</selector> 

讓我知道它是如何爲你工作的。

+0

它沒有爲我工作。我認爲這是日食問題。我在最後一個問題的下面提出了一些額外的問題,你可以看看,並告訴我如何擺脫那個問號? – user3473222 2015-02-11 13:28:05

0

用這個替換你的xml。

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<item 

    android:drawable="@drawable/camera_button_before" /> 

<item 
    android:state_pressed="true" 
    android:drawable="@drawable/camera_button_after"/> 

+0

我之前做過,現在再次..但它沒有工作..我認爲這是日食問題。我在最後一個問題的下面提出了一些額外的問題,你可以看看,並告訴我如何擺脫那個問號? – user3473222 2015-02-11 13:28:30

+0

確保您的按鈕是可點擊的。並讓你設置按鈕上的點擊監聽器? – justDroid 2015-02-11 14:18:53

+0

+ _ + ;;我已經這樣做了......但是謝謝〜 – user3473222 2015-02-11 14:32:39