Android: how to get ImageView to fill screen

2020-02-10 14:48发布

问题:

I don't know how to get an image to fill the screen, have tried everything, maybe there is some particular layout I should be using..?

I'm using an ImageView, however this just fills about 70% of the screen, leaving blank space at the bottom...

At the moment i'm just using a LinearLayout and setting its background to my image. While this fills the screen, the image is slightly streched, which I don't want.

Any help would be greatly appreciated! :)

The code inside the ImageView tags of my layout file are:

android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:scaleType="centerCrop" 
android:adjustViewBounds="true" 
android:src="@drawable/image" 

回答1:

//in your xml Image attribute as

android:scaleType="fitXY"

or //using thru programatically

imgview.setScaleType(ScaleType.FIT_XY);

Note: this applies to when the image is set with android:src="..." rather than android:background="..." as backgrounds are set by default to stretch and fit to the View.



回答2:

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    android:adjustViewBounds="true"
    android:src="@drawable/image"

just change layout width and height which would work with both 'center' and 'centercrop'



回答3:

Use the below line in your Image View xml code.

android:scaleType="fitXY"


回答4:

Dont forget to remove

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

from the layoutparams, at the top of the xml of the activity. Otherwise the image will have problem filling the entire screen.



回答5:

You can use practically any layout and just set the background image to your image. Then just make width and height fill parent.

or instance if you want just an image set it for your main linear layout

    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:orientation="horizontal"
      android:layout_height="fill_parent"
      android:background="@drawable/yourimage">
</LinearLayout>