Android 使显示的图像只能在imagebutton中单击

Android 使显示的图像只能在imagebutton中单击,android,onclicklistener,imagebutton,Android,Onclicklistener,Imagebutton,我的活动中有一个ImageButton,如下所示: <ImageButton android:contentDescription="@string/controller" android:id="@+id/control" android:layout_width="match_parent" android:layout_weight="3" android:layout_height="0dp" android:scaleType="fitCe

我的
活动中有一个
ImageButton
,如下所示:

 <ImageButton 
   android:contentDescription="@string/controller"
   android:id="@+id/control"
   android:layout_width="match_parent"
   android:layout_weight="3"
   android:layout_height="0dp"
   android:scaleType="fitCenter"
   android:background="@android:color/transparent"
   android:src="@drawable/displayed_image" />

关于所需的一些UI标准化,我无法将图像宽度分配给任何对象,但
匹配\u父对象
,因此,生成的图像如下所示:

我只想让显示的图像是图像的可点击部分,而整个透明区域是不可点击的,因此当为
图像按钮
分配
onClickListener
时,仅当点击在显示的图像中时才执行操作,有没有任何静态或编程的方式可以负担得起


为什么不将imageButton包装在一个具有匹配父级宽度的RelativeLayout中,然后将imageButton设置为“包装内容”和“中心属性”

您可以尝试使用以下方法:-但老实说,还有其他方法可以做到这一点

还有一些其他想法:

  • 容器中有3个单独的图像。左边和右边的图片以及你的可点击图片。组合起来,它们看起来与原来的单一
    ImageView
    完全相同。这样做的好处是,您可以控制中心部件,而忽略任何其他触摸/点击。这可能需要一个
    RelativeLayout
    。我不知道这是否违背了你对用户界面的担忧

  • 覆盖不同的视图(可能是ImageButton?),以便为您处理单击。因此,实际上,您最终会得到类似:[[]](括号代表每个视图-两个视图中较大的一个是您的原始视图)


  • 希望这能有所帮助。

    您可以将图像按钮的高度和宽度设置为
    wrap\u content
    。然后添加属性android:layout\u gravity=“center”
    。仅此而已。

    您的ImageButton的容器类型是什么?这是一个线性布局吗,相对的。。。?你能发布所有相关的xml吗?@Plinio.Santos linearlayout和handler方法,你是如何设置的?@Plinio.Santos是一个普通的
    new OnClickListener()
    不幸的是,这并不能阻止用户点击OP所要求的“红色”区域之外的内容。是的,正如@LokiSinclair所说,这对你是没有帮助的。我已经发布了
    mach_parent
    ,这应该是
    wrap_content
    。但是,由于所有设备之间的标准化,您提供给我的链接无法与我一起工作,因此我无法在UI中处理任何数字,其他两种方法将是一种很好的解决方法,但看起来就像摆在您面前的答案(特别是第一种)所以我选择第一个作为最早答案的正确答案。你不必证明你接受答案是正确的:)任何对你有用的东西!很高兴你把它整理好了!