我需要在 RelativeLayout 的中心放置一个网格,但我不想使用 Gravity 只是因为我需要使用 leftMargin 和 topMargin 将其放在特定位置。
网格是使用
我尝试使用此代码的两个 LinearLayout 制作的,但我在屏幕的左上角看到了网格:
RelativeLayout fl = new RelativeLayout(this);
RelativeLayout.LayoutParams flp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
fl.setLayoutParams(flp);
fl.setBackgroundResource(R.drawable.background);
//Linear Layout to contain grid rows
LinearLayout grid_layout = new LinearLayout(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
llp.leftMargin = (int)Util.convertDpToPixel(200, this);
llp.topMargin = (int)Util.convertDpToPixel(200, this);
//Set Params
grid_layout.setOrientation(LinearLayout.VERTICAL);
grid_layout.setId(12);
fl.addView(grid_layout, llp);
Log.d("Display", "" + grid_layout.getHeight());
//LinearLayout(s) ROWS
for (int i = 0; i < 4; i++)
{
LinearLayout ll = new LinearLayout(this);
llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(llp);
grid_layout.addView(ll);
//Cells
for (int j = 0; j < 4; j++) //Creating 4 cell blocks in a row
{
TextView tv = new TextView(this);
tv.setBackgroundResource(R.drawable.cell_shape);
tv.setHeight((int)Util.convertDpToPixel(42, this));
tv.setWidth((int)Util.convertDpToPixel(42, this));
ll.addView(tv);
grid[i][j] = new CellBlock(grid, i, j, tv, null); //Building the game grid
}
}
似乎这两行:
llp.leftMargin = (int)Util.convertDpToPixel(200, this);
llp.topMargin = (int)Util.convertDpToPixel(200, this);
不工作!为什么一直把网格放在左上角?
谢谢