Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView won't fill parent

I have a ScrollView on one of my screens. I want the right edge to have a shadow. I decided the easiest way to do this was to make the child of the ScrollView a RelativeLayout, and have two children of the RelativeLayout -- one being a LinearLayout that will house the layout of the screen, and the second View being the shadow.

Like so...

<ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scrollbars="none" >

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <!-- stuff -->
                </LinearLayout>

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:src="@drawable/shadow"
                    android:layout_alignParentRight="true"
                    />
            </RelativeLayout>
        </ScrollView>

Unfortunately, this doesn't quite work. The ImageView is forcing its dimensions to be the size of the image file. It will not stretch vertically to be the height of the RelativeLayout. I've also tried "match_parent" to no avail. The image is a 9-patch.

Ideas?

like image 670
Andrew Avatar asked Jul 16 '12 19:07

Andrew


2 Answers

Applying drawable content as the source of an ImageView somewhat carries with it an inherent requirement that you want the view to do what it can to accomodate the content without modifying the content itself very much. Typically, this is the behavior you would want out of an ImageView.

What you really want is the behavior you get by setting drawable content as the background of a view, for which you don't really need ImageView at all. A background is designed to simply stretch, fill, etc. to whatever size the view is. Also, since you are using RelativeLayout you can tell the view to match the bound of the view you are shadowing by adding an id and some extra layout_alignparameters.

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/content_layout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
            <!-- stuff -->
            </LinearLayout>

            <View
                android:layout_width="11dp"
                android:layout_height="fill_parent"
                android:layout_alignParentRight="true"
                android:layout_alignTop="@id/content_layout"
                android:layout_alignBottom="@id/content_layout"
                android:background="@drawable/shadow"
                />
        </RelativeLayout>
like image 162
devunwired Avatar answered Nov 15 '22 15:11

devunwired


try this

<ImageView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:background="@drawable/ic_launcher"
                android:scaleType="fitXY"
                android:layout_alignParentRight="true"
                />

here is what I get

enter image description here

and code id

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:scrollbars="none" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <!-- stuff -->
        </LinearLayout>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:background="@drawable/ic_launcher"
            android:scaleType="fitXY" />
    </RelativeLayout>
</ScrollView>

like image 34
Mohsin Naeem Avatar answered Nov 15 '22 15:11

Mohsin Naeem