【发布时间】:2015-11-06 11:28:16
【问题描述】:
我正在尝试创建一个包含 16 x 16 按钮的 GridLayout,但我无法删除按钮之间的空格。
我看到了this,但没用。
这是我现在正在尝试的:
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
GridLayout v = (GridLayout)findViewById(R.id.myGrid);
v.setPadding(0, 0, 0, 0);
v.setUseDefaultMargins(false);
v.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
v.setRowOrderPreserved(false);
Random r = new Random();
for (int row = 0; row < 16; row++)
for (int col = 0; col < 16; col++) {
GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
lp.setMargins(0, 0, 0, 0);
lp.rowSpec = GridLayout.spec(row);
lp.columnSpec = GridLayout.spec(col);
lp.width = 40;
lp.height = 40;
Button b = new Button(this);
b.setWidth(40);
b.setHeight(40);
b.setText(Integer.toString(r.nextInt(10)));
b.setPadding(0, 0, 0, 0);
b.setLayoutParams(lp);
v.addView(b, lp);
}
}
截图
知道有什么问题吗?
编辑:我将一些按钮的背景更改为绿色图片,这就是我得到的:
显然间距是按钮图像的一部分...为什么是 Android,为什么?
【问题讨论】:
标签: android spacing android-gridlayout