Android 布局 - ImageView 聚焦,但屏幕上不显示任何内容(无突出显示)

发布于 2024-09-26 14:19:08 字数 168 浏览 6 评论 0原文

如果我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰魂雪魄 2024-10-03 14:19:08

您可以使用带有选择器的可绘制对象作为背景。

例如,我在 res/drawable 中有 gallery_image_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
          android:constantSize="true">
  <item android:state_selected="true">
    <shape android:shape="rectangle">
      <solid android:color="#E6E4E0"/>
      <stroke android:width="3dp" android:color="#F9C11E"/>
      <padding android:left="3dp"
               android:top="3dp"
               android:right="3dp"
               android:bottom="3dp" />
    </shape>
  </item>
  <item android:state_pressed="true">
    <shape android:shape="rectangle">
      <solid android:color="#E6E4E0"/>
      <stroke android:width="3dp" android:color="#F9C11E"/>
      <padding android:left="3dp"
               android:top="3dp"
               android:right="3dp"
               android:bottom="3dp" />
    </shape>
  </item>
  <item>
    <shape android:shape="rectangle">
      <solid android:color="#E6E4E0"/>
      <stroke android:width="3dp" android:color="#FFF"/>
      <padding android:left="3dp"
               android:top="3dp"
               android:right="3dp"
               android:bottom="3dp" />
    </shape>
  </item>
</selector>

这会在图像周围创建三个不同颜色的 3dp 线。它们是可见的,因为填充迫使图像在稍小的区域中渲染。

在代码中使用如下:

image.setBackgroundResource(R.drawable.gallery_image_bg)

You can use a drawable with a selector as the background.

For example, I have gallery_image_bg.xml in res/drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
          android:constantSize="true">
  <item android:state_selected="true">
    <shape android:shape="rectangle">
      <solid android:color="#E6E4E0"/>
      <stroke android:width="3dp" android:color="#F9C11E"/>
      <padding android:left="3dp"
               android:top="3dp"
               android:right="3dp"
               android:bottom="3dp" />
    </shape>
  </item>
  <item android:state_pressed="true">
    <shape android:shape="rectangle">
      <solid android:color="#E6E4E0"/>
      <stroke android:width="3dp" android:color="#F9C11E"/>
      <padding android:left="3dp"
               android:top="3dp"
               android:right="3dp"
               android:bottom="3dp" />
    </shape>
  </item>
  <item>
    <shape android:shape="rectangle">
      <solid android:color="#E6E4E0"/>
      <stroke android:width="3dp" android:color="#FFF"/>
      <padding android:left="3dp"
               android:top="3dp"
               android:right="3dp"
               android:bottom="3dp" />
    </shape>
  </item>
</selector>

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:

image.setBackgroundResource(R.drawable.gallery_image_bg)
尘曦 2024-10-03 14:19:08

快速的解决方案是使用一个侦听器,它将响应单击,并在 imageView 上设置橙色边框...

imageView.setOnClickListener(listener)

但是您必须对所有 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...

imageView.setOnClickListener(listener)

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.

十年不长 2024-10-03 14:19:08

迟到的答案,但您也可以使用 LayerDrawable 作为图像源。然后你就不必搞乱背景、填充等。

LayerDrawable d = new LayerDrawable(new Drawable[]{new BitmapDrawable(myBmp), getResources().getDrawable(R.drawable.my_selector_list)});
imageView.setImageDrawable(d);

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.

LayerDrawable d = new LayerDrawable(new Drawable[]{new BitmapDrawable(myBmp), getResources().getDrawable(R.drawable.my_selector_list)});
imageView.setImageDrawable(d);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文