0

我有一个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);
    }

}
4

1 回答 1

1

您正在设置类型的a的CellView子类。在方法的定义中,该方法接收类型的参数并且不包含边距属性,因此这可能是问题的原因。ViewlayoutParamsLinearLayout.LayoutParamsViewsetLayoutParamsViewGroup.LayoutParamsViewGroup

您可以尝试子类LinearLayout化而不是View.

于 2012-07-13T11:55:55.340 回答