LinearLayout buttonsView = new LinearLayout(this); buttonsView.setOrientation(LinearLayout.VERTICAL); for (int r = 0; r < 6; ++r) { Button btn = new Button(this); btn.setText("A"); // Set layout parameters with weight and add margins LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, 0, // This will make the button’s height proportional based on weight 1.0f // Weight is 1 for each button, distributed evenly ); lp.setMargins(20, 10, 20, 10); // Add margins (left, top, right, bottom) buttonsView.addView(btn, lp); } // Set layout parameters for the main LinearLayout to fill the screen ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT ); setContentView(buttonsView, lp); |