Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView: scale properly above Button

I'm new to Android and everything went well until now I have to deal with the layout. What I want is this layout:

Layout how it should be: The image should be properly scaled and keep its ratio.

That's why I tried this XML code, but it doesn't work the way I want. The image keeps its ratio but the left and right side are out of the screen when vertical and cut at the top and bottom when horizontal:

Layout how it is: enter image description here

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:orientation="vertical"
    android:background="@color/background">


     <ImageView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:src="@drawable/logo"
        android:gravity="center"
        android:scaleType="centerCrop"
        android:contentDescription="@string/app_logo"
        android:layout_weight="2.5" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:gravity="center">  

        <Button
            android:id="@+id/id1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:text="@string/text1"
            android:textSize="40sp" 
        />

          <Button
            android:id="@+id/id2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:text="@string/text2"
            />

        <Button
            android:id="@+id/id3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:layout_gravity="center"
            android:text="@string/text3" />
    </LinearLayout>
</LinearLayout>

I googled a lot but just could not find what I was looking for. Please help!

like image 437
JustABit Avatar asked Dec 12 '25 16:12

JustABit


1 Answers

It looks like android:scaleType="fitCenter" is what you are looking for

like image 92
Vladimir Mironov Avatar answered Dec 15 '25 07:12

Vladimir Mironov