26

谁能告诉我如何调整大小imageButton以完全适合图像?这是我尝试过的代码,但图像放置在我使用 定位的位置android:scaleType,但我无法减小imageButton. 请帮我纠正这个问题。我尝试的代码是:

<ImageButton>
android:id="@+id/Button01"
android:scaleType="fitXY" // i have tried all the values for this attr
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="false"
android:paddingLeft="10dp"
android:src="@drawable/eye"> // this is the image(eye)
</ImageButton>
4

8 回答 8

32
android:background="@drawable/eye"

自动工作。

android:src="@drawable/eye"

是我使用按钮的宽度和高度调整图像大小的所有问题......

于 2011-03-21T03:42:39.723 回答
15

您正在使用属性“src”设置图像

android:src="@drawable/eye">

使用“ background”属性代替“ src”属性:

android:background="@drawable/eye"

喜欢:

<ImageButton
  android:id="@+id/Button01"
  android:scaleType="fitXY" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:cropToPadding="false"
  android:paddingLeft="10dp"
  android:background="@drawable/eye"> // this is the image(eye)
</ImageButton>
于 2010-07-23T17:21:14.117 回答
3

您可能必须以编程方式调整按钮的大小。您需要在 onCreate() 方法中显式加载图像,并在那里调整按钮的大小:

public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.main);  
  ImageButton myButton = (ImageButton) findViewById(R.id.button);  
  Bitmap image = BitmapFactory.decodeResource(R.drawable.eye);  
  myButton.setBitmap(image);  
  myButton.setMinimumWidth(image.getWidth());  
  myButton.setMinimumHeight(image.getHeight());
  ...
}

根据 setMinimumX 的规范,它不能保证工作(因为宽度和高度仍然取决于父视图),但它应该适用于几乎所有情况。

于 2010-07-23T16:32:48.870 回答
2

尝试使用 ScaleType centerInside。

ScaleType 在 Eclipse 布局设计器中未正确呈现,因此请在您正在运行的应用程序中进行测试。

于 2011-06-01T15:23:17.283 回答
2

您不必使用 src 属性来使用它

错误的方式(图像不适合按钮)

android:src="@drawable/myimage"

正确的方法是使用背景属性

<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/skin" />

皮肤是xml

皮肤.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- <item android:drawable="@drawable/button_disabled" android:state_enabled="false"/> -->
    <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
    <!-- <item android:drawable="@drawable/button_focused" android:state_focused="true"/> -->
    <item android:drawable="@drawable/button_normal"/>

</selector>

使用 button_pressed.png 和 button_normal.png

这也将帮助您创建具有pressed、normal、disabled 和focused 4 种状态的蒙皮按钮。确保所有 png 的大小保持相同

于 2013-10-23T10:10:47.053 回答
2

您是否尝试给 layout_width 和 layout_height 如下所示?由于您使用 wrap_content 进行设置,因此图像按钮会扩展为源图像的高度和宽度的大小。

<blink>    
  <ImageButton>
    android:id="@+id/Button01"
    android:scaleType="fitXY" 
    android:layout_width="80dip"
    android:layout_height="80dip"
    android:cropToPadding="false"
    android:paddingLeft="10dp"
    android:src="@drawable/eye"> 
  </ImageButton>
</blink>
于 2011-06-30T17:08:58.543 回答
1

你也可以设置背景是透明的。所以这个按钮看起来很适合你的图标。

 <ImageButton
    android:id="@+id/Button01"
    android:scaleType="fitcenter" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:cropToPadding="false"
    android:paddingLeft="10dp"
    android:background="@android:color/transparent"
    android:src="@drawable/eye" />
于 2012-05-23T01:16:22.890 回答
0

我认为您已经解决了这个问题,并且正如其他答案所建议的那样

android:background="@drawable/eye"

可用。但是我更喜欢

android:src="@drawable/eye"
android:background="00000000" // transparent

它也很好用。(当然,以前的代码会将图像设置为背景,而另一个会将图像设置为图像)但是根据您选择的答案,我猜您的意思是 9-patch。

于 2012-10-17T06:17:15.270 回答