0

如果我像这样向它添加图像,如何显示 ImageButton 被按下:

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

        button.setImageDrawable( testPic ); 

并制作透明背景,以便只有图像:

button.setBackgroundColor(Color.TRANSPARENT); 

所以当我按下图像按钮时,我没有看到它被按下。我希望该图像在按下时会突出显示或其他内容。

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

那么有什么想法吗?

谢谢。

4

3 回答 3

2

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

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

于 2012-05-07T07:22:07.777 回答
1

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

于 2012-05-07T07:21:53.000 回答
0

您必须在运行时更改图像。您可以在 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 复制到您的可绘制文件夹

于 2012-05-07T07:24:48.333 回答