Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use weightsum = 3 for linearlayout, and layout_weight = 1 for each button. See the detail code. I hope this will help. Good luck :)</p> <pre><code>public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.HORIZONTAL); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); layout.setLayoutParams(params); layout.setWeightSum(3); for (int i = 0; i &lt; 3; i++) { LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); btnParams.weight = 1.0f; Button btn = new Button(this); btn.setText("Btn"); btn.setLayoutParams(btnParams); layout.addView(btn); } setContentView(layout); } } </code></pre> <p>Or this code for better but more complicated ^^</p> <pre><code>public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout mainLayout = new LinearLayout(this); mainLayout.setOrientation(LinearLayout.HORIZONTAL); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); mainLayout.setLayoutParams(params); mainLayout.setWeightSum(3); for (int i = 0; i &lt; 3; i++) { LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); Button btn = new Button(this); btn.setText("Btn"); btn.setLayoutParams(btnParams); LinearLayout childLayout = new LinearLayout(this); LinearLayout.LayoutParams childParam = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); childParam.weight = 1.0f; childLayout.setLayoutParams(childParam); childLayout.setGravity(Gravity.CENTER); childLayout.addView(btn); mainLayout.addView(childLayout); } setContentView(mainLayout); } } </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