视图Layout_Margin在FrameLayout中不起作用

时间:2012-07-13 11:44:22

标签: android margin android-framelayout

我有一个frameLayout,在这个布局中有几个视图,我想将每个视图从顶部边缘到特定距离。 我已经尝试了以下代码,但似乎它不起作用

   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类:

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

}

1 个答案:

答案 0 :(得分:1)

您设置为CellView的子类ViewlayoutParams类型LinearLayout.LayoutParams。在View中方法的definition中,方法setLayoutParams接收ViewGroup.LayoutParams类型的参数,而ViewGroup不包含保证金属性,因此可能成为问题的原因。

您可以尝试继承LinearLayout而不是View