简体   繁体   中英

Imageview not matching the width of Linearlayout

I have a layout in which a linearlayout contains imageview as its direct child. I want to fit the width of this imageview so that it spans horizontally to match the size of the parent which will take the width of the whole screen.

How can I do that? This is what i've done so far:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/linearLayout"
        >


        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_margin="0dp"
            android:src="@drawable/policemen_salute"
            android:id="@+id/banner"
            />



    </LinearLayout>



</RelativeLayout>

It looks like this

在此处输入图片说明

Thanks!

You can use android Scaletype Property .

android:scaleType="fitXY"

Then set android adjustviewbounds true from XML .Try this way I hope it will helps you.

Add the 'scaletype' attribute:

<ImageView
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:scaleType="fitXY"
        android:layout_margin="0dp"
        android:src="@drawable/policemen_salute"
        android:id="@+id/banner"/>

See http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

You have to use a scaleType to scale correctly the image. try your ImageView like this:

<ImageView android:layout_width="fill_parent" android:layout_height="100dp" android:layout_margin="0dp" android:src="@drawable/policemen_salute" android:id="@+id/banner" android:scaleType="centerCrop"/>

If you wish to scale your image, you might want to add

android:scaleType="fitXY"

(Scale in X and Y independently, so that src matches dst exactly) to your ImageView

<ImageView
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_margin="0dp"
            android:scaleType="fitXY"
            android:src="@drawable/policemen_salute"
            android:id="@+id/banner"
            />

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