簡體   English   中英

按下按鈕時背景發生變化,無法正常工作

[英]Background change on button when pressed, isn't working properly

這是我的按鈕的xml文件:

<?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/play_pressed" />
<item android:drawable="@drawable/play" />
</selector>

但是,這只能使用一次,當我去另一項活動然后返回時,則無法使用。

當我第一次運行該活動時,按鈕的背景會按應有的方式更改,但是當我轉到另一個活動並返回到上一個活動時,則在按下按鈕時背景更改不起作用。

編輯:

問題出在我的Java代碼中。 它每次都將背景替換為靜態png圖像(第一次除外)...上面的代碼按預期工作。

它應該像下面這樣:

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

<item 
 android:state_selected="false"
    android:state_pressed="false" 
    android:drawable="@drawable/play_default" />
<item android:state_pressed="true" 
    android:drawable="@drawable/play_pressed" />
<item android:state_selected="true"
 android:state_pressed="false" 
    android:drawable="@drawable/play_pressed" />
</selector>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM