title: Android: how to scale a layout with screen size
body: <p>Consider this layout (pulled from <a href="http://www.androidradar.net/?p=126" rel="nofollow noreferrer">here</a>):</p>
  
  <p><img src="https://i.stack.imgur.com/fFvHC.png" alt="enter image description here"></p>
  
  <p>I'd like to understand the principles behind making this layout scale with screen size.  For the square, a custom onMeasure function works nicely:</p>
  
  <pre><code>@Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      super.onMeasure(widthMeasureSpec, widthMeasureSpec);
  }
  </code></pre>
  
  <p>The width and height of the custom Togglebuttons and Imagebuttons below should scale to fill the remainder of the screen, minus layout_margins, and the text and images within should scale to fill the buttons, minus padding.  The outer margin should also scale.</p>
  
  <p>My first thought was to use a relative layout to position the buttons, and layout_margin/padding attributes to create margins.  However, relative layouts and layout_margin/padding require fixed pixel values, so they aren't scalable.</p>
  
  <p>I then thought of using nested linear layouts with layout_weights to position the buttons, and placeholder views to create margins.  Although these techniques are scalable, they don't work with buttons, because buttons have many attributes (text size, image size, corner radius, etc.) that require fixed pixel values.  This limitation means, for example, that the following xml:</p>
  
  <pre><code>&lt;ToggleButton 
      android:layout_weight="1"
      style="@style/myButton"
      [...] /&gt;
  &lt;View 
      android:layout_weight="1"/&gt;
  &lt;ImageButton 
      android:layout_weight="1"
      style="@style/myButton"
      [...] /&gt;
  </code></pre>
  
  <p>won't necessarily make the two buttons, and the space between them, all the same width.  It all depends on the text size, image size, etc. etc. of the buttons.</p>
  
  <p>I've taken a look at <a href="https://stackoverflow.com/questions/8916363/how-to-make-buttons-automatically-scale-down-when-screen-size-decrease">this question</a> but I feel there should be a simpler solution for such a simple problem, that shouldn't require resorting to too much non-XML.  </p>