title: LinearLayout with weight-attributes on children inside a ScrollView
body: <p>The usual question regarding ScrollViews seems to be how to use fillViewport to let the ScrollView stretch to the whole screen. My problem is similar, but going the other way :)</p>
  
  <p>I got a LinearLayout that contains multiple elements. All except one element have a fixed height, the special one is an ImageView that should stretch to fill the remaining space. This works fine :) </p>
  
  <p>Now I want to put the LinearLayout into a ScrollView, as some of my elements should expand at runtime (e.g. click on "more"-icon that expands a TextView). In the unexpanded version I would like to have all elements fit on the screen, as if the ScrollView wouldn't exist. </p>
  
  <p>Unfortunately, when wrapping the ScrollView around the LinearLayout, the ImageView is scaled to maxSize und the screen does not fit the screen. Do you have any ideas how to achieve my goal?</p>
  
  <p>I reproduced the problem in an example app to share it with you:</p>
  
  <pre><code>&lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fillViewport="true" &gt;
  
      &lt;LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical" &gt;
  
          &lt;ImageView
              android:layout_width="fill_parent"
              android:layout_height="0px"
              android:layout_weight="1"
              android:background="#ffff0000"
              android:scaleType="fitCenter"
              android:src="@drawable/background" /&gt;
  
      &lt;TextView
          android:layout_width="fill_parent"
          android:layout_height="80dp"
          android:background="#ff00ff00"
          android:text="element 2"
          tools:context=".MainActivity" /&gt;
  
          &lt;TextView
              android:layout_width="fill_parent"
              android:layout_height="80dp"
              android:background="#ff00ff00"
              android:text="element 1"
              tools:context=".MainActivity" /&gt;
      &lt;/LinearLayout&gt;
  
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p>Here are the screenshots from the two versions:</p>
  
  <p><img src="https://i.stack.imgur.com/FmE9vl.png" alt="Screenshot inside ScrollView (notice the scrollbar and cropping on element 1)"> <img src="https://i.stack.imgur.com/fcoNWl.png" alt="Screenshot without ScrollView"></p>
  
  <p>The one above is the Layout with ScrollView (notice the scrollbar and cropping on element 1),  the one below without.</p>
  
  <p><strong>Update:</strong> The original height of the image in the image-view is bigger than the screen. So it should be downscaled (that's why it has weight 1 and scaleType set).</p>
  
  <p><strong>Solution: The following code solved the problem for me (based on Luksprog answer)</strong></p>
  
  <p>Main Activity (excerpt, change of layout is induced by click on ImageView):</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      scrollView = (ScrollView) findViewById(R.id.scroller);
      frame = (FrameLayout) findViewById(R.id.frame);
      layout = (LinearLayout) findViewById(R.id.layout);
  
      final ImageView image = (ImageView) findViewById(R.id.image);
      image.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              ImageView image = (ImageView) findViewById(R.id.image);
              frame.removeView(layout);
              scrollView.addView(layout);
              image.getLayoutParams().height = image.getHeight();
          }
      });
  
  }
  </code></pre>
  
  <p>Layout XML file</p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/frame"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
       &gt;
  
      &lt;ScrollView 
          android:id="@+id/scroller"
          android:layout_height="fill_parent"
          android:layout_width="fill_parent"
          android:fillViewport="true"
          &gt;
  
      &lt;/ScrollView&gt;
  
      &lt;LinearLayout
          android:id="@+id/layout"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical" &gt;
  
          &lt;ImageView
              android:id="@+id/image"
              android:layout_width="fill_parent"
              android:layout_height="0px"
              android:layout_weight="1"
              android:background="#ffff0000"
              android:scaleType="fitCenter"
              android:src="@drawable/background" /&gt;
  
          &lt;TextView
              android:id="@+id/text1"
              android:layout_width="fill_parent"
              android:layout_height="80dp"
              android:background="#ff00ff00"
              android:text="element 2"
              tools:context=".MainActivity" /&gt;
  
          &lt;TextView
              android:layout_width="fill_parent"
              android:layout_height="80dp"
              android:background="#ff00ff00"
              android:text="element 1"
              tools:context=".MainActivity" /&gt;
          &lt;TextView
              android:layout_width="fill_parent"
              android:layout_weight="0.1"
              android:layout_height="0px"
              android:background="#ff00ff00"
              android:text="element 3"
              tools:context=".MainActivity" /&gt;
      &lt;/LinearLayout&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>