简体   繁体   中英

How to show that ImageButton is pressed?

How to show that ImageButton is pressed if i add image to it like this:

        Drawable testPic = getResources().getDrawable(R.drawable.test_pic);

        button.setImageDrawable( testPic ); 

And make transparent background so that there would be just image:

button.setBackgroundColor(Color.TRANSPARENT); 

So when i press image button i don't see that it is pressed. I want that image would be highlighted or something when it is pressed.

Also every my button is created dynamically by code and i don't know what image would be and how many button there are.

So any ideas ?

Thanks.

you should use a selector for choosing all the different appearances of a UI element, including an ImageButton .

first comes the state_Pressed=true which means what does it look like when pressed. after that comes the regular which is the normal state of the button.

我认为您应该使用状态列表 ...

You have to change Image at run time. You can achieve these with the help of xml file.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@drawable/category_listing_bg_img" /> 
<item android:state_pressed="true"   android:drawable="@drawable/category_listing_bg_img_pressed" /> 
</selector>

Copy this xml to your drawable folder

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