简体   繁体   中英

Android: RelativeLayout issue

I need to place a grid in the center of a RelativeLayout, but I don't want to use Gravity just because I need to put it on a specific position, using leftMargin and topMargin.

The grid is made using two LinearLayout's
I tried using this code, but I see the grid on the top left corner of the screen:

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
        }
    }

It seems that these two lines:

llp.leftMargin = (int)Util.convertDpToPixel(200, this);
llp.topMargin = (int)Util.convertDpToPixel(200, this);

does not work! Why it keep to place the grid in the top left corner?

Thank you

You have to use

RelativeLayout.LayoutParams llp = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
llp.setMargins((int)Util.convertDpToPixel(100, this), (int)Util.convertDpToPixel(200, this), (int)Util.convertDpToPixel(200, this), (int)Util.convertDpToPixel(200, this));

instead of

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);

for your RelativeLayout "grid_layout"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM