开发者

Stretch image in FrameLayout to match neighboring element's height?

An instance of ImageView in a RelativeLayout with height set to fill_parent will stretch as the RelativeLayout does. E.g. if an EditText in the same layout is 10 lines tall, the image will stretch to 10 lines tall.

How can this behavior be replicated in a FrameLayout? The image's height set to fill_parent seems to behave the same as wrap_content, regardless of the height of the other items in the Fr开发者_开发知识库ameLayout.

I'd prefer to handle this purely in the XML.


I found a (possibly hack) workaround- wrapping the FrameLayout in a LinearLayout let me set the FrameLayout to fill_parent and so its children could stretch accordingly. So:

<LinearLayout
orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <FrameLayout
        android:measureAllChildren="true"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <ImageView
            android:src="@drawable/image"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:layout_gravity="right|center_vertical" />
    </FrameLayout>
</LinearLayout>
<RelativeLayout ... >
    ... other stuff ...
</RelativeLayout>
</LinearLayout>

And the ImageView will stretch to the height of the EditText.


FrameLayout has all it's children views overlaid on the same area. Depending on your settings, you can change how the views are displayed. According to Android's FrameLayout,

The size of the frame layout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits).

Also, from Common Layout Objects

FrameLayout is the simplest type of layout object. It's basically a blank space on your screen that you can later fill with a single object — for example, a picture that you'll swap in and out. All child elements of the FrameLayout are pinned to the top left corner of the screen; you cannot specify a different location for a child view. Subsequent child views will simply be drawn over previous ones, partially or totally obscuring them (unless the newer object is transparent).

In your example, the image in a FrameLayout will not be stretched to the dimensions of it's largest sibling, irrespective of using fill_parent or wrap_content.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜