Android 布局 - ImageView 聚焦,但屏幕上不显示任何内容(无突出显示)
如果我将 ImageView 设置为可点击和可聚焦,它就可以工作,但我无法知道哪个图像被聚焦。让它在视图周围绘制橙色边框以便用户知道它当前处于焦点的最佳方法是什么?
我尝试将其放入 LinearLayout 中并将其设置为可聚焦和可点击,但没有任何运气。即使我在上面留了边距,也没有任何迹象表明它已被选中。
If I set my ImageView to be both clickable and focusable, it works, but I have no way to tell which image is focused. What's the best way to get it to draw an orange border around the view so the user knows it's currently in focus?
I tried dropping it in a LinearLayout and setting that to be focusable and clickable instead, but didn't have any luck. Even when I put a margin on it, there's nothing to indicate that it was selected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用带有选择器的可绘制对象作为背景。
例如,我在 res/drawable 中有 gallery_image_bg.xml:
这会在图像周围创建三个不同颜色的 3dp 线。它们是可见的,因为填充迫使图像在稍小的区域中渲染。
在代码中使用如下:
You can use a drawable with a selector as the background.
For example, I have gallery_image_bg.xml in res/drawable:
This creates three different-colored 3dp lines around the image. They are visible because the padding forces the image to render in a slightly smaller area.
Used in code like so:
快速的解决方案是使用一个侦听器,它将响应单击,并在 imageView 上设置橙色边框...
但是您必须对所有 ImageView 项目执行此操作。至少他们可以共享同一个听众。
长期的解决方案是实现您自己的 View,扩展 ImageView,它会绘制自己的橙色边框以响应单击。
我假设您希望它像单选按钮一样工作,单击 ImageView 将从之前选择的 ImageView 中删除边框?因此,您将需要共享侦听器来维护对当前选定 ImageView 的引用,然后在单击新 ImageView 时可以“取消选择”该 ImageView。
The quick solution is to use a listener, which will respond to the click, and set an orange border on the imageView...
But you have to do that for all the ImageView items. At least they can share the same listener.
The long solution is to implement your own View, extending ImageView, which draws its own orange border in response to a click.
I assume you want this to work like radio buttons, where clicking on an ImageView will remove the border from the previously selected ImageView? So you will need the shared listener to maintain a reference to the currently selected ImageView, which can then be "de-selected" when a new ImageView is clicked.
迟到的答案,但您也可以使用 LayerDrawable 作为图像源。然后你就不必搞乱背景、填充等。
Late answer, but you can also use a LayerDrawable for the image source. Then you don't have to mess around with the background, padding, etc.