Adham
Adham

Reputation: 64854

View Layout_Margin doesn't work inside the FrameLayout

I have a frameLayout and inside this layout there are several Views, and I want to margin each view from the top to a specific distance . I have tried the following code, but it seems that it doesn't work

   FrameLayout lytBoard = (FrameLayout) findViewById(R.id.lytBoard);
   LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
   params.setMargins((int)board.cells.get(i).x1, (int)board.cells.get(i).y1, 0, 0);
   CellView cv = new CellView(getApplicationContext());
   cv.setLayoutParams(params);
   lytBoard.addView(cv);

Cell View class:

public class CellView extends View {

    public CellView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
        setMeasuredDimension(size, size);
    }

}

Upvotes: 0

Views: 346

Answers (1)

Jonathan Naguin
Jonathan Naguin

Reputation: 14766

You are setting to the CellView, subclass of View, a layoutParams of the type LinearLayout.LayoutParams. In the definition of the method in View, the method setLayoutParams receives params of the type ViewGroup.LayoutParams and ViewGroup does not contains a margin property, so this may be the cause of the problem.

You can try to subclass LinearLayout instead of View.

Upvotes: 1

Related Questions