我想以编程方式向布局添加多个按钮。 但是,按钮的数量每次都不同,我只是希望它们以包裹内容宽度彼此相邻放置。填充一行后,它应该转到下一行并继续这样。
实现这一目标的最简洁方法是什么?
感谢。
答案 0 :(得分:0)
LinearLayout verticalLayout = new LinearLayout(context);
verticalLayout.setOrientation(LinearLayout.VERTICAL);
while(isActive) {
LinearLayout horziontalLayout = new LinearLayout(context);
horziontalLayout.setOrientation(LinearLayout.HORIZONTAL);
add buttons here..
//Example Button button = new Button(context);
//horziontalLayout.addView(button);
verticalLayout.addView(horziontalLayout);
isActive = false // when youe done filling up buttons..
}
答案 1 :(得分:0)
最简单的方法应该是添加多个水平LinearLayout的垂直LinearLayout
//vertical one
LinearLayout vlinear = new LinearLayout(this);
vlinear.setOrientation(LinearLayout.VERTICAL);
LayoutParams vlinearParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
vlinear.setLayoutParams(vlinearParams);
parentgroup.addView(vlinear);
//horizontal lines
for (int i=0;i<numlines){
LinearLayout hlinear = new LinearLayout(this);
hlinear.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams vlinearParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
hlinear.setLayoutParams(hlinearParams);
vlinear.addView(hlinear);
///add the loop for adding cells
for...
hlinear.addView(cell);
}