如何将图像嵌入图像按钮缩放到图像的大小

How to have an image embedded on an image button scale to the size of the image

我正在尝试创建一个计算器应用程序,我想使用一个图像按钮来允许用户select他们想要计算的内容。我想将此图像用于 0 按钮 (zero button image). Currently when I add the image to the user interface, it is cropped like this (cropped zero image)。当我调整按钮大小时,有没有办法让图像缩放到按钮的大小?谢谢!

代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lontronix.calculator.Calculator">

    <TextView

        android:id="@+id/textView"
        android:layout_width="500dp"
        android:layout_height="125dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="@string/Filler_Text"
        android:textSize="35pt" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="250dp"
        android:layout_height="150dp"
        app:srcCompat="@drawable/zero"
        android:layout_marginStart="50dp"
        android:layout_marginBottom="42dp"
        android:layout_alignParentBottom="true"
        />

</RelativeLayout>

将这些属性写入您的 imageView。

android:adjustViewBounds="true"
android:scaleType="fitXY"

或者您可以尝试不同的 scaleType,见下文:

https://robots.thoughtbot.com/android-imageview-scaletype-a-visual-guide