简体   繁体   English

如何显示已按下ImageButton?

[英]How to show that ImageButton is pressed?

How to show that ImageButton is pressed if i add image to it like this: 如果我向其中添加图像,如何显示ImageButton被按下:

        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 . 您应该使用选择器来选择UI元素的所有不同外观,包括ImageButton

first comes the state_Pressed=true which means what does it look like when pressed. 首先是state_Pressed=true ,这表示按下时的样子。 after that comes the regular which is the normal state of the button. 之后是常规状态,这是按钮的正常状态。

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

You have to change Image at run time. 您必须在运行时更改Image。 You can achieve these with the help of xml file. 您可以借助xml文件来实现这些目标。

<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 将此xml复制到您的可绘制文件夹中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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