title: Position TextView directly below an ImageView that takes up as much space as possible without cropping
body: <p>I'm having a lot of trouble getting this to work correctly. I have an <code>ImageView</code> and a <code>TextView</code>, both in a <code>LinearLayout</code>. I want the <code>ImageView</code> to take up as much of the parent view as possible without cropping or changing the aspect ratio, but leaving enough room for the TextView directly below it. I have this mostly working, but when the height of the image is small enough to leave extra space in the parent view, the TextView only appears at the very bottom of the <code>LinearLayout</code>, as opposed to just below the image.</p>
  
  <p>I've tried many combinations of layout parameters, fiddling with the gravity, weight, height, and width of both the ImageView and the TextView. I even tried using a <code>RelativeLayout</code>, but the results were very inconsistent.</p>
  
  <p>Here is the relevant portion of my code:  </p>
  
  <pre><code>// Create a vertical linear layout to hold the image with the caption below
  // it, taking up as much space on the screen as possible
  LinearLayout ll = new LinearLayout(context);
  ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
  ll.setOrientation(LinearLayout.VERTICAL);
  ll.setGravity(Gravity.CENTER);
  
  // Get the image view
  ImageView img = new ImageView(context);
  LayoutParams ilp = new LayoutParams(LayoutParams.MATCH_PARENT, 0);
  ilp.weight = 1;
  ilp.gravity = Gravity.CENTER;
  img.setLayoutParams(ilp);
  
  // Create the caption view
  TextView cap = new TextView(context);
  cap.setText("Example caption text");
  LayoutParams clp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  clp.weight = 0;
  clp.gravity = Gravity.CENTER_HORIZONTAL;
  cap.setLayoutParams(clp);
  
  // Add views to the linear layout
  ll.addView(img);
  ll.addView(cap);
  
  // Load the image using an AsyncTask
  loadImageTask = new LoadCachedImageTask(context, img, pos);
  loadImageTask.execute("image src");
  </code></pre>
  
  <p>Here's an image of the "good behaviour," where the image is scaled up to the point where the text is still visible:</p>
  
  <p><img src="https://i.imgur.com/TovCr.jpg" alt="good behaviour"></p>
  
  <p>Here's an image of the "bad behaviour," where there is room above and below the image, but the text still stays at the bottom:</p>
  
  <p><img src="https://i.imgur.com/h75Rg.png" alt="bad behaviour"></p>