Android中的ImageButton

Nan*_*l T 24 android imagebutton

任何人都可以告诉我如何调整imageButton的大小以使图像精确拟合.这是我尝试的代码,但图像放置在我定位的位置, imageButton但我无法减小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>
Run Code Online (Sandbox Code Playgroud)

小智 32

android:background="@drawable/eye"
Run Code Online (Sandbox Code Playgroud)

自动运作.

android:src="@drawable/eye"
Run Code Online (Sandbox Code Playgroud)

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


Jor*_*sys 13

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

android:src="@drawable/eye">
Run Code Online (Sandbox Code Playgroud)

使用" background"属性代替" src"属性:

android:background="@drawable/eye"
Run Code Online (Sandbox Code Playgroud)

喜欢:

<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>
Run Code Online (Sandbox Code Playgroud)


inf*_*nda 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());
  ...
}
Run Code Online (Sandbox Code Playgroud)

根据 setMinimumX 的规范,它不能保证工作(因为宽度和高度仍然依赖于父视图),但它应该在几乎所有情况下都能很好地工作。