问题
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