0

There is an ImageButton button

<ImageButton
  android:id="@+id/imageButton"
  android:layout_width="24dp"
  android:layout_height="24dp"
  android:layout_gravity="center"
  android:onClick="onClickMenu"
  app:srcCompat="@drawable/ic_baseline_point_white" />

I upload a 24dp-by-24dp image to it. I increase the width of the button to 48dp, the height remains the same, the picture is also stretched.

How can I make the size of the image in the button always remain unchanged?

Here is the original image of the image in the button

enter image description here

Set

android:layout_width="48dp"
android:scaleType="fitCenter"

I got this image enter image description here

Another option

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="36dp"
  android:layout_marginTop="4dp"
  android:layout_weight="1"
  android:orientation="horizontal">
  <TextView
    android:id="@+id/text_title_01"
    android:layout_width="match_parent"
    android:layout_height="36dp"
    android:layout_marginStart="12dp"
    android:layout_marginTop="0dp"
    android:layout_marginEnd="8dp"
    android:layout_weight="1"
    android:inputType="textMultiLine"
    android:paddingTop="8dp"
    android:text="@string/txt_title_01"
    android:textColor="@color/white"
    android:textSize="18sp"
    android:textStyle="bold" />
    <RelativeLayout
      android:id="@+id/imageButtonContainer"
      android:layout_width="48dp"
      android:layout_height="24dp"
      android:onClick="onClickMenu">
      <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_gravity="center"                                           
        android:src="@drawable/ic_baseline_point_white" />

     </RelativeLayout>
</LinearLayout>

It turns out this picture enter image description here

alex
  • 324
  • 1
  • 8
  • 28

1 Answers1

0

I don't know if a Button supports that, but what you would typically do is wrap a RelativeLayout around it, like this:

<RelativeLayout
    android:id="@+id/imageButtonContainer"
    android:layout_width="48dp"
    android:layout_height="24dp"
    android:onClick="onClickMenu">
    
    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_gravity="center"
        android:src="@drawable/ic_baseline_point_white" />

</RelativeLayout>
Cactusroot
  • 1,019
  • 3
  • 16