Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set button layout_gravity="fill_vertical" programmatically?
    text
    copied!<p>I got a <code>GridLayout</code>, and two or more button inside, and all the buttons must with same width. My problem is that when the buttons having different line number of text, the <code>marginTop</code> of button will different too. How can I fix this? I want all the buttons in the same position of height. </p> <p>Thanks.</p> <p>I already try to set <code>Gravity</code> with <code>FILL_VERTICAL</code>, but not working.</p> <pre><code>GridLayout grid_neighbor = (GridLayout) findViewById(R.id.grid_neighbors); grid_neighbor.setColumnCount(4); for (i = 0; i &lt; namelist.length; ++i) { Button temp = new Button(this); temp.setText(namelist[i]); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); lp.gravity = Gravity.FILL_VERTICAL; temp.setLayoutParams(lp); //temp.setGravity(Gravity.FILL_VERTICAL); temp.setWidth(button_size); temp.setHeight(button_size); grid_neighbor.addView(temp); } </code></pre> <p>Here is the picture:</p> <p><img src="https://i.stack.imgur.com/ToqPi.png" alt="My picture"></p> <p>This is now working too.</p> <pre><code>GridLayout grid_neighbor = (GridLayout) findViewById(R.id.grid_neighbors); grid_neighbor.setColumnCount(4); for (i = 0; i &lt; namelist.length; ++i) { Button temp = new Button(this); temp.setText(namelist[i]); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.ALIGN_TOP, temp.getId()); temp.setLayoutParams(lp); //temp.setGravity(Gravity.FILL_VERTICAL); temp.setWidth(button_size); temp.setHeight(button_size); grid_neighbor.addView(temp); } </code></pre> <p>This will work! Thanks.</p> <pre><code>GridLayout.Spec speccolumn = GridLayout.spec(i%4); GridLayout.Spec specrow = GridLayout.spec(i/4, GridLayout.TOP); grid_neighbor.addView(temp, new GridLayout.LayoutParams(specrow, speccolumn)); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload