我想彼此相邻添加图像按钮,一旦到达 LinearLayout 的末尾,它应该进入下一行。
注意:API 级别 10
我可能需要做什么,但我不知道如何:
- 计算父宽度的结束
- 添加按行号计算的上边距值
- 将图像按钮设置在父母的左侧
如果有更好的方法,请发表。
这是我的测试代码:
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams paramsLO = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
paramsLO.setMargins(0, 0, 0, 0);
paramsLO.height = 150;
paramsLO.width = 150;
for (int i = 0; i < 20;i++)
{
ImageButton imgBtn = new ImageButton(this);
imgBtn.setBackgroundColor(Color.TRANSPARENT);
imgBtn.setImageResource(R.drawable.placeholder);
imgBtn.setAdjustViewBounds(true);
layout.addView(imgBtn,paramsLO);
}
layout.setGravity(Gravity.LEFT | Gravity.CENTER_HORIZONTAL);
addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));