使显示的图像只能在图像按钮中单击

时间:2014-07-16 12:37:08

标签: android onclicklistener imagebutton

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

 <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 标准化要求我无法将图像宽度分配给match_parent以外的任何内容,因此,生成的图像如下:

我想要做的只是显示的图片是图片的可点击部分,透明区域的整体不是可点击,以便在为onClickListener分配ImageButton时,仅当点击位于显示图片时执行的操作时,是否有任何操作静态或程序化的方式来承担吗?

enter image description here

3 个答案:

答案 0 :(得分:1)

你可以尝试使用这样的东西:http://blahti.wordpress.com/2012/06/26/images-with-clickable-areas/ - 但说实话,还有其他方法可以做到。

其他一些想法:

  1. 容器内有3个单独的图像。左右片和可点击的图片。结合它们看起来与原始单ImageView完全相同。好处是,您可以控制中心部分并忽略任何其他触摸/点击。这可能需要RelativeLayout。我不知道这是否与你的UI问题有关。

  2. 覆盖不同的视图(可能是ImageButton?),为您处理点击。所以实际上你最终会得到类似的东西:[[]](括号代表每个视图 - 两者中较大的一个是原作)。

  3. 希望这有帮助。

答案 1 :(得分:1)

您可以将图像按钮的高度和宽度设置为wrap_content。然后添加属性android:layout_gravity="center"。就这么。

答案 2 :(得分:0)

为什么不将imageButton包装在RelativeLayout中,它具有match_parent宽度,然后将imagebutton设置为wrap_content和centerInParent

相关问题