title: Android Custom layout gravity
body: <p>I am trying to write my own layout class which places it children in a regular grid. 
  The layout itself works nicely, but I can't get the text on the buttons inside this layout to be centered. 
  When I place the same buttons in a LinearLayout the button text is centered as desired, so the fault is probably within my layout. 
  But how can my layout affect the gravity of the text on its child views? I suspect it has something to do with the layout parameters but I don't really know how this works yet.</p>
  
  <p>Here are some code snippets which might be relevant to the problem:</p>
  
  <p>My layout class WeightedGridLayout:</p>
  
  <pre><code>public class WeightedGridLayout extends ViewGroup {
  
  // ...
  
  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
      for (int i = 0, N = getChildCount(); i &lt; N; i++) {
          View c = getChildAt(i);     
          // ...
          // do some calculation
          //
          c.layout( childLeft, childTop, childRight, childBottom );
      }       
  }
  
  public static class LayoutParams extends ViewGroup.MarginLayoutParams  {
      public Position position = position( 0, 0 );
      public Span span = span( 1, 1 );
      // Position and Span are local classes which are irrelevant here
  
  public LayoutParams( Position position, Span span ) {
      super(FILL_PARENT, FILL_PARENT);
      this.position = position;
      this.span = span;
  }
  public LayoutParams( Position position ) {
      this( position, span(1,1) );
  }
  public LayoutParams() {
      this( position(0,0), span(1,1) );
  }
  public LayoutParams(MarginLayoutParams params) {
      super(params);
  }
  public LayoutParams(LayoutParams that) {
      super(that);
      this.position = that.position;
      this.span = that.span;
  }
  public LayoutParams(Context context, AttributeSet attrs) {
      super(context, attrs);
  }
  
  }
  </code></pre>
  
  <p>The class is used like this:</p>
  
  <pre><code>  WeightedGridLayout grid = (WeightedGridLayout) findViewById(R.id.mainMenu);
    LayoutInflater inflater = getLayoutInflater();
    Button button = (Button)inflater.inflate(R.layout.buttonproperties, null);
    button.setText( "None" );
    WeightedGridLayout.Position pos = WeightedGridLayout.position(colIdx,rowIdx);
    WeightedGridLayout.LayoutParams lp = new WeightedGridLayout.LayoutParams(pos);
    lp.setMargins(5,20,5,20);         
    grid.addView(button, lp );
  </code></pre>
  
  <p>Here are the properties for the button:</p>
  
  <pre><code>&lt;Button xmlns:android="http://schemas.android.com/apk/res/android"
      android:background="@drawable/default_button"
      android:gravity="center"
      android:textSize="@dimen/text"
      android:textColor="@color/text" &gt;
  &lt;/Button&gt;
  </code></pre>
  
  <p>The button text appears at the top of the button rather than in the center as it should. 
  What do I have to do in order to get the text to the center?</p>