Android 在单击时突出显示图像按钮
我正在使用 ImageButton
。但点击时我没有突出显示。我用谷歌搜索,许多人建议使用显示另一张图像的选择器。有什么办法解决这个问题吗?仅使用一张图像并突出显示它或赋予它发光效果。以便用户知道该按钮已被单击。
I am using an ImageButton
. But I don get the highlight when clicked. I googled and many suggested to use selector where another image is displayed. Is there any way around this. by using only one image and highlighting it or giving it a glow effect. so that the user knows that button has been clicked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这其实并不是很难做到的。您甚至不需要创建 2 个单独的 .png 文件或类似文件。例如,如果您想要一个具有渐变的按钮,然后在按下按钮时更改它:
第 1 步:
创建默认按钮渐变 (drawable/default_button.xml):
步骤 2:创建默认按钮按下渐变 (drawable/default_button_pressed.xml):
步骤 3:创建选择器 (drawable/default_button_selector.xml):
步骤 4 (可选)< /em>:为按钮创建样式 (values/style.xml):
步骤 5:使用按钮 (layout/main.xml):
如您所见,这并不是特别困难。
This is actually not very difficult to do. You don't even need to create 2 seperate .png files or anything like that. For instance, if you want to have a button which has a gradient, and then change it when the button is pressed:
Step 1:
Create default button gradient (drawable/default_button.xml):
Step 2: Create default button pressed gradient (drawable/default_button_pressed.xml):
Step 3: Create selector (drawable/default_button_selector.xml):
Step 4 (optional): Create style for the button (values/style.xml):
Step 5: use the button (layout/main.xml):
As you can see, it's not particularly difficult to do.
无需创建多个图像(按下、正常等),甚至无需创建选择器。使用 setOnTouchListener 而不是 setOnClickListener。下面的代码将为您提供单击项目上的灰色叠加层。
Without having to create multiple images (pressed, normal etc) and even don't have to create selector. Use setOnTouchListener rather than setOnClickListener. The below code will give you the grey overlay on the clicked item.
您只需在
View
中使用android:foreground
即可实现可点击效果:对于深色主题,还需添加主题到你的
布局
(使可点击效果清晰):You can simply use
android:foreground
for yourView
to achieve clickable effect:For use with dark theme add also theme to your
layout
(to make clickable effect clear):为了不必为每个按钮设置多个可绘制对象,我在触摸侦听器中设置了图像按钮的滤色器属性。
请参阅另一篇文章中的代码:
https://stackoverflow.com/a/14278790/891479
In order not to have to set several drawable for each button I set the color filter attribute of the image button in an on touch listener.
See code here in another post:
https://stackoverflow.com/a/14278790/891479
我成功做到了这一点!
首先,在drawable文件夹中声明buttonStyle.xml。
然后在drawable文件夹中创建button_pressed.xml。
之后,您在drawable文件夹中创建button_normal.xml。
您可以使用默认颜色,但如果您想像我一样这样做,您需要在值文件夹中有一个名为 colors.xml 的文件,并在其中包含此值:
最后,您将其设置为按钮或其他内容:
注意我设置了颜色透明的描边,因为当您设置backgroundresource时,您的按钮会变得更大,并且通过这个技巧它仍然保持原始状态。
这是我能够以编程方式实现此目的的唯一方法。
如果您想通过 XML 来执行此操作:
I managed to do this!!
First you declare buttonStyle.xml in drawable folder.
Then you create button_pressed.xml in drawable folder.
After that, you create button_normal.xml in drawable folder.
You can use default colors but if you want to do it like me, you need to have a file named colors.xml in values folder and this values inside:
Finally you set this to your button or whatever:
NOTICE THAT I set a stroke with color transparent because when you set backgroundresource your button becomes bigger and with this trick it remains original.
This is the only way I could archieve this programmatically.
If you want to do this by XML: