Android 视图布局';不要在框架布局内工作

Android 视图布局';不要在框架布局内工作,android,margin,android-framelayout,Android,Margin,Android Framelayout,我有一个frameLayout,在这个布局中有几个视图,我想将每个视图从顶部留到一个特定的距离。 我试过下面的代码,但似乎不起作用 FrameLayout lytBoard = (FrameLayout) findViewById(R.id.lytBoard); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height); params.setMargins((int)board

我有一个
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);
单元视图类:

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

}

您正在设置
CellView
View
的子类,即
LinearLayout.layoutParams
类型的
layoutParams
。在
View
中的方法中,方法
setLayoutParams
接收类型为
ViewGroup的参数。LayoutParams
ViewGroup
不包含边距属性,因此这可能是问题的原因


您可以尝试子类化
线性布局
而不是
视图

在框架布局中添加视图不是一种好的做法。您不能,而只能在相对布局中添加视图