Android的布局 - ImageView的重点,但不显示在屏幕上任何东西(无亮点)但不、布局、重点、东西

2023-09-08 08:38:57 作者:何必太在乎你

如果设置我的ImageView既可点击和可聚焦的,它的工作原理,但我没有办法知道哪些图像聚焦。什么是得到它的周围画视图橙色的边框,这样用户就知道它是当前处于焦点的最佳方式?

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?

我想在一个的LinearLayout下降,并设置了要成为焦点和点击的代替,但没有任何运气。甚至当我把保证金就可以了,没有什么可以表明它被选中。

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.

推荐答案

您可以用绘制与选择作为背景。

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

例如,我在水库gallery_image_bg.xml /绘制:

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>

这创建图像周围三个不同颜色的线条3DP。它们是可见的,因为填充迫使图像中的略微较小的区域来呈现

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.

用于在code,像这样:

Used in code like so:

image.setBackgroundResource(R.drawable.gallery_image_bg)