ImageButton 图像在添加背景属性时缩放,同时使用 scaletype 定位图像

ImageButton image scales when adding background attribute, while using scaletype to position image

我在使用 ImageButton 中的可绘制对象时遇到问题。

我正在尝试对齐图像按钮右侧的白色箭头(见下图)。我通过使用 scaleType="fitEnd".

在不使用背景颜色的情况下成功地做到了这一点

            <ImageButton
            android:layout_width="250dp"
            android:layout_height="40dp"
            android:id="@+id/imageButton7"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/arrowUp"
            android:scaleType="fitEnd" />

但是,当尝试添加背景颜色时,箭头图像会放大,因此看起来不像预期的那样(如下图所示)。

            <ImageButton
            android:layout_width="250dp"
            android:layout_height="40dp"
            android:id="@+id/imageButton7"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/arrowUp"
            android:scaleType="fitEnd"
            android:background="#054365" />

有没有办法在使用 scaleType="fitEnd" 时更改 ImageButton 的背景颜色的同时获得正确的箭头大小和定位,还是我必须使用其他方法?

尝试

android:backgroundTint="#054365" 

而不是

android:background="#054365"

更改按钮颜色。这是一个简单的技巧。

有关详细信息,请参阅 Material effect on button with background color

<ImageButton
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:src="Your image path"
    android:scaleType="fitEnd"
    android:backgroundTint="#FFFFFF"/>

"backgroundTint" 属性将在 API 21 级及更高级别起作用。如果低于 API 级别 21,应用程序将忽略该属性,这不是错误。