Android : 图片按钮有不需要的边距

Android : Image button has unwanted margin

我的 android 项目中有一些图像按钮。但它们有一些像这张照片一样的灰色边距(我从真实设备上拍摄这张照片):

我怎样才能删除它们?? 我使用了这段代码,但没有任何效果。

 ib.setAdjustViewBounds(true);     

刚刚在 java 中定义的图像按钮,我在 xml 上没有它们。

 ib = new ImageButton(this);
 ib.setImageResource(R.drawable.image);
 ib.setAdjustViewBounds(true);
 ib.setLayoutParams(ibrllp);

如何删除这个额外的灰色边距?

将您的图像按钮背景设置为空。例如

ib.setBackground(null);

默认情况下它有一个带边距的背景。

在代码中:

ImageButton imageButton = ImageButton.class.cast(rootView.findViewById(R.id.imageButton));
imageButton.setBackground(null);

在 xml 文件中:

<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton"
        android:background="@null" />

"rootView" 是膨胀为片段的布局或 activity。