![](/img/trans.png)
[英]Android Studio (Java): Changing Color of a Button directly with java not .xml
[英]Have a Button with 3 different background images. Android Studio, XML, Java
有人可以幫我,告訴我這段代碼有什么問題嗎? 我有 3 張不同的小屋背景圖片(綠色、灰色、正常),在下面的 Java 代碼中,我檢查哪個狀態是活動的
您可以在不同條件下直接設置按鈕的圖像,如下所示:-
((ImageButton)shackButton).setImageResource(R.drawable.<imageid>);
您可以在不創建任何其他資源文件的情況下執行此操作。
將您的 xml 更改為此
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shack1000green" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/shack1000grey" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@drawable/shack1000" android:state_focused="false" android:state_pressed="false"/>
</selector>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.