简体   繁体   中英

Have a Button with 3 different background images. Android Studio, XML, Java

can somedy help me, and tell me whats wrong in this code? I have 3 different background pictures of my shack, (green, grey, normal) and in the Java Code below i check which state is active

编码

You can directly set the image of button in different conditions like this:-

((ImageButton)shackButton).setImageResource(R.drawable.<imageid>);

You can do it without creating any other resource file.

change your xml to this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM