Android:展开 ImageView 可点击区域

发布于 2024-12-19 05:54:17 字数 514 浏览 3 评论 0原文

我有一个小图像显示在 LinearLayout 中,右侧和左侧有一些边距。 我的问题是边距不可点击。 如何在视图的右侧和左侧显示一个空间,使用这个额外的空间来扩展我的可点击区域?

<ImageButton
    android:id="@+id/markReadButton"
    android:layout_width="22dp"
    android:layout_height="22dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:adjustViewBounds="true"
    android:background="@android:drawable/checkbox_on_background"
    android:gravity="center_vertical"
    android:onClick="onMarkRead" >
</ImageButton>

I have a small image displayed in a LinearLayout with some margin on its right ans left.
My problem is that the margin isn't clickable.
How can I display a space on the right and left of my View, using this extra space to expand my clickable zone ?

<ImageButton
    android:id="@+id/markReadButton"
    android:layout_width="22dp"
    android:layout_height="22dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:adjustViewBounds="true"
    android:background="@android:drawable/checkbox_on_background"
    android:gravity="center_vertical"
    android:onClick="onMarkRead" >
</ImageButton>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

撧情箌佬 2024-12-26 05:54:17

这是在 Google IO 2011 Schenduling 应用程序中使用 TouchDelegate 完成的。看看像这样使用它的Activity;

// Larger target triggers star toggle
final View starParent = mRootView.findViewById(R.id.header_session);
FractionalTouchDelegate.setupDelegate(starParent, mStarred, new RectF(0.6f, 0f, 1f, 0.8f));

TouchDelegate 实现在这里实现。它是开源的,应该很容易导入到您的项目中。

祝你好运,希望这有帮助!

This is done in the Google IO 2011 Schenduling app, using a TouchDelegate. Take a look at the Activity that uses it like this;

// Larger target triggers star toggle
final View starParent = mRootView.findViewById(R.id.header_session);
FractionalTouchDelegate.setupDelegate(starParent, mStarred, new RectF(0.6f, 0f, 1f, 0.8f));

The TouchDelegate implementation is implemented here. It is open source and should be easy to import into your project.

Good luck, hope this helps!

绮筵 2024-12-26 05:54:17

我认为使用填充而不是边距可以让您为您的项目提供更多空间,同时也允许它可点击。

看看边距与填充的描述:视图的填充和边距之间的差异

编辑:
尝试使用 android:src 作为图像,设置 android:background="@null" 并增加 ImageButton 对象的整体大小。那时,我什至不确定您是否需要填充。您可以根据需要修改以 ImageButton 为中心的 src 图像的高度和宽度。

    <ImageButton
        android:id="@+id/markReadButton"
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:src="@android:drawable/checkbox_on_background"
        android:background="@null"
        android:adjustViewBounds="true"
        android:onClick="onMarkRead"
    />

I think using padding instead of the margin will allow you to give your item more space but also allow it to be clickable.

Have a look at this description of margins vs padding: Difference between a View's Padding and Margin

Edit:
Try using android:src for the image, setting android:background="@null" and increasing the overall size of the ImageButton object. At that point, I'm not even sure you'll need the padding at all. You can just modify the height and width as needed with the src image centered in the ImageButton.

    <ImageButton
        android:id="@+id/markReadButton"
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:src="@android:drawable/checkbox_on_background"
        android:background="@null"
        android:adjustViewBounds="true"
        android:onClick="onMarkRead"
    />
一人独醉 2024-12-26 05:54:17

只需添加布局父级并设置例如 20dp 更大的宽度和高度尺寸

<RelativeLayout android:id="@+id/relativeLayoutButton"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:paddingRight="20dp"
        android:paddingLeft="20dp" >

<ImageButton
    android:id="@+id/markReadButton"
    android:layout_width="22dp"
    android:layout_height="22dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:adjustViewBounds="true"
    android:background="@android:drawable/checkbox_on_background"
    android:gravity="center_vertical"
    android:onClick="onMarkRead" >
</ImageButton>

,然后设置布局和图像的相同位置。
设置还可点击相同的布局和按钮

Just add Layout parent and set eg 20dp greater size of width and height

<RelativeLayout android:id="@+id/relativeLayoutButton"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:paddingRight="20dp"
        android:paddingLeft="20dp" >

<ImageButton
    android:id="@+id/markReadButton"
    android:layout_width="22dp"
    android:layout_height="22dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:adjustViewBounds="true"
    android:background="@android:drawable/checkbox_on_background"
    android:gravity="center_vertical"
    android:onClick="onMarkRead" >
</ImageButton>

And next set the same posision of layout and your image.
set also clickable same layout and button

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文