简体   繁体   中英

How can I make my image bigger on Android with XML?

I have an image on my ImageView but I don't know how to make it bigger with XML . My image it's the following:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/img"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:src="@drawable/image"/>

I tried changing android:layout_width and android:layout_height attributes but if I put them with a number (for example, 200dp width and 100dp height ) the ImageView it's on the left of the screen and not center.

I want to center the image and makes it a little bit bigger than the original. I couldn't find anything to makes it bigger at the same time it is displayed on the center of the screen.

Is it possible? How can I do that?

Thanks in advance!

使用scaletype并在图像视图部分添加android:adjustviewbounds="true"

Keep the size you want as the width and height of the ImageView and change the gravity of its parent to center

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/img"
        android:layout_marginTop="40dp"
        android:src="@drawable/image"/>

</LinearLayout>

EDIT:

If this is what you want your layout to look like

在此处输入图片说明

then all you would need to do is add

android:layout_gravity="center_horizontal"

to your ImageView , and specify the width and height in dp

There are 3 attributes you can add to your imageview:

android:scaleType=""
android:scaleX=""
android:scaleY=""

ScaleType types can be seen here . Basically, you'll want to use CENTER_CROP .

scaleX and scaleY uses float as parameters, defining the scale for the two axis. See the reference here .

Both codes above works ( Andrew Brooke and IntelliJ Amiya , Thanks!) but now I found a "trick" to avoid both of these methods. It's to create the ImageView using android:minWidth and android:minHeight so the final ImageView will be like:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/img"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:minWidth="100dp"
    android:minHeight="70dp"
    android:src="@drawable/image"/>

With that, you can center your ImageView with the size that you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM