繁体   English   中英

如何显示已按下ImageButton?

[英]How to show that ImageButton is pressed?

如果我向其中添加图像,如何显示ImageButton被按下:

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

        button.setImageDrawable( testPic ); 

并设置透明背景,以便只有图像:

button.setBackgroundColor(Color.TRANSPARENT); 

因此,当我按下图像按钮时,我看不到它被按下了。 我希望该图像在按下时会突出显示。

另外,我的每个按钮都是通过代码动态创建的,我不知道它将是什么图像以及有多少个按钮。

有什么想法吗?

谢谢。

您应该使用选择器来选择UI元素的所有不同外观,包括ImageButton

首先是state_Pressed=true ,这表示按下时的样子。 之后是常规状态,这是按钮的正常状态。

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

您必须在运行时更改Image。 您可以借助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>

将此xml复制到您的可绘制文件夹中

暂无
暂无

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

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