Try это так:
LinearLayout lyt = (LinearLayout)findViewById(R.id.mylayout);
LinearLayout buttonsLinearLayout = new LinearLayout(context);
buttonsLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
for(i=0;i<=3 ;i++)
{
ImageButton ib= new ImageButton(this);
BitmapDrawable imagebd;
ib.setClickable(true);
imageid = getResources().getIdentifier("drawable/" + image,null,getPackageName());
ib.setBackgroundResource(imageid);
buttonsLinearLayout.addView(ib);
}
lyt.addView(buttonsLinearLayout);
Таким образом, вы все еще есть основной вертикальный LinearLayout, поэтому вы можете помещать вещи под кнопки.
EDIT: использовать его для более чем 1 ряд, я сделал простую математическую выч ...
LinearLayout lyt = (LinearLayout) findViewById(R.id.mylayout);
// calculate the number of rows needed
int numOfButtons = something you already have i guess;
// row layout
LinearLayout buttonsLinearLayout = new LinearLayout(context);
;
for (i = 0; i <= numOfButtons; i++) {
// for every 3 rows, create a new layout
// and add it to the main linear layout
if (i % 3 == 0) {
// create layout for a 3 button row
buttonsLinearLayout = new LinearLayout(context);
buttonsLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
// add the new row with 3 buttons to the main lineal layout
lyt.addView(buttonsLinearLayout);
}
ImageButton ib = new ImageButton(this);
BitmapDrawable imagebd;
ib.setClickable(true);
imageid = getResources().getIdentifier("drawable/" + image, null, getPackageName());
ib.setBackgroundResource(imageid);
buttonsLinearLayout.addView(ib);
}
}
не проверял, дайте мне знать, если это работает для вас ...
добавить то внутри новой линейной компоновки – Shahar