【发布时间】:2013-07-10 11:17:54
【问题描述】:
我想将图像按钮彼此相邻添加,一旦到达 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));
【问题讨论】: