Java 如何调整imagebutton可单击空间的背景大小

Java 如何调整imagebutton可单击空间的背景大小,java,android,imagebutton,Java,Android,Imagebutton,我有一个自定义图像按钮,我只希望自定义图像按钮是可点击的。目前,按钮周围的所有区域都是“可点击”的,并启动新活动。我想最小化这个“可点击的背景”空间。我怎么做 <ImageButton android:id="@+id/start_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="t

我有一个自定义图像按钮,我只希望自定义图像按钮是可点击的。目前,按钮周围的所有区域都是“可点击”的,并启动新活动。我想最小化这个“可点击的背景”空间。我怎么做

 <ImageButton
    android:id="@+id/start_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@null"
    android:src="@drawable/flashcardbutton" />

虽然我对此不确定,但只是猜测而已。。是否可以使用:

public void getHitRect(Rect outRect) {
    outRect.set(getLeft()-10, getTop()-10, getRight()-10, getBottom()-10);
}
好吧,让我们来试一试,我发现
ImageButton
有一个带有此签名的方法

public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
类有这个方法

public void setBoundsInScreen (Rect bounds)
这么说吧
我认为你应该实现你想要做的事情,这是对@Sushil answer的补充。也许这有助于:

<ImageButton
    android:id="@+id/start_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@null"
    android:adjustViewBounds="true" <!-- Here is the thing you need -->
    android:src="@drawable/flashcardbutton" />

android:src=“@drawable/flashcardbutton”/

所以,我想这肯定是我的错。即使我使背景“透明”,框架仍然比我的图像大。我只需要使用photoshop裁剪图像。谢谢大家!

您不想使用图像按钮将图像作为'src',您想使用常规按钮将图像作为'background',否则它会模仿带有填充的按钮等等,即使您将背景图像置零

像这样使用原始图像:

 <Button
    android:id="@+id/start_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/flashcardbutton" />


你能发布你的xml和可能的图片吗?这样我们就对你有了更多的了解?另外,我假设您对
宽度
使用了
wrap\u content
?@codeMagic是的,我对这两个属性都使用了wrap\u content。您的
按钮将可单击。如果
图像按钮
中有您不想点击的空间,则需要将其从
按钮
中删除。如果您不想点击的是父级
布局
,那么我们需要查看that@codeMagic我已经清除了图像的背景。一个合适的屏幕截图可以解释更多的问题,你可以吗?你的问题是你真的不需要使用ImageButton,您希望使用一个常规按钮,用图像替换其背景。检查我的答案。