Massive unwanted whitespace with LinearLayout in S

2019-08-07 02:41发布

问题:

I have a layout with a custom ImageView, and two TextEdits; one above and one below. To make this fit on multiple screens I have surrounded the whole thing with a ScrollView.

However, when the ScrollView is added a massive gap (approx. the height of the screen) appears between the top TextEdit and the ImageView, and the ImageView and the bottom TextEdit.

I am only overriding the onDraw() method in the ImageView (and am still calling super.onDraw() from there)

This is my layout:

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

    <EditText
        android:id="@+id/top_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/top_hint" >

        <requestFocus />
    </EditText>

    <com.javanut13.gememerator.MImageView
        android:id="@+id/image_viewer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="0dp"
        android:src="@drawable/ic_action_search" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/bottom_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:layout_weight="3"
            android:hint="@string/bottom_hint" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/button_text" />
    </LinearLayout>
</LinearLayout>

回答1:

Ok, it turned out that my ImageView wasn't resizing its height when it scaled the image, and so it would keep the height of the raw image (which was from my camera; about 2000 pixels tall) and thus have a massive space above and below it.

I came across this question: (Image do not resize with ImageView) which says to add android:adjustViewBounds="true" to the ImageView, which fixes the problem.