简体   繁体   English

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

[英]Android layout - ImageView focused, but doesn't show anything on screen (no highlight)

If I set my ImageView to be both clickable and focusable, it works, but I have no way to tell which image is focused. 如果我将ImageView设置为可单击和可聚焦,则可以使用,但是无法分辨聚焦的图像。 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. 我尝试将其放置在LinearLayout中,并将其设置为可聚焦和可单击,但没有任何运气。 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. 您可以使用带有选择器的可绘制对象作为背景。

For example, I have gallery_image_bg.xml in res/drawable: 例如,我在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>

This creates three different-colored 3dp lines around the image. 这将在图像周围创建三个不同颜色的3dp线。 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)

Late answer, but you can also use a LayerDrawable for the image source. 答案较晚,但是您也可以将LayerDrawable用于图像源。 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);

The quick solution is to use a listener, which will respond to the click, and set an orange border on the imageView... 快速解决方案是使用侦听器,该侦听器将对单击做出响应,并在imageView上设置橙色边框...

imageView.setOnClickListener(listener)

But you have to do that for all the ImageView items. 但是您必须对所有ImageView项执行此操作。 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. 长久的解决方案是实现您自己的View,扩展ImageView,该ImageView会在单击时绘制自己的橙色边框。

I assume you want this to work like radio buttons, where clicking on an ImageView will remove the border from the previously selected ImageView? 我假设您希望它像单选按钮一样工作,单击ImageView会从先前选择的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. 因此,您将需要共享的侦听器来维护对当前所选ImageView的引用,然后在单击新ImageView时可以“取消选择”该引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM