Image inside ImageButton not scaling to fill button

青春壹個敷衍的年華 提交于 2019-12-12 04:35:07

问题


My layout includes this ImageButton as shown:

But as you can see, the image is not taking the full size of the ImageButton.

This is the layout file where the ImageButton is defined:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@mipmap/dashboard_profile"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="71dp"
    android:layout_marginStart="71dp"
    android:layout_marginTop="100dp"
    android:id="@+id/imageButton3"
    android:scaleType="fitXY"/>

As you can see, I already added android:scaleType="fitXY", but it's not working.

I also tried to change the button size in Android Studio's design mode, but it's not letting me change.

How can I solve these problems?


回答1:


Unlike Imageview, Imagebutton has a padding around the image area. This is intentional. This is because of the default button style. You can do one of the following:

  • Use a borderless button style as your background.
    style="?android:attr/borderlessButtonStyle"

  • Set your background as null.
    android:background="@null"




回答2:


Try to use android:src instead of app:srcCompat

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:src="@mipmap/dashboard_profile"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="71dp"
    android:layout_marginStart="71dp"
    android:layout_marginTop="100dp"
    android:id="@+id/imageButton3"
    android:scaleType="fitXY"/>



回答3:


You can use this code

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@mipmap/dashboard_profile"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="71dp"
    android:layout_marginStart="71dp"
    android:layout_marginTop="100dp"
    android:id="@+id/imageButton3"
    android:scaleType="fitXY"/>


来源:https://stackoverflow.com/questions/41450099/image-inside-imagebutton-not-scaling-to-fill-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!