繁体   English   中英

View Layout_Margin在FrameLayout中不起作用

[英]View Layout_Margin doesn't work inside the FrameLayout

我有一个frameLayout并且在此布局内有几个Views,我想将每个View的边缘从顶部到特定距离。 我已经尝试了以下代码,但似乎不起作用

   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.LayoutParamslayoutParams View中的方法定义中,方法setLayoutParams接收类型为ViewGroup.LayoutParams参数,并且ViewGroup不包含margin属性,因此这可能是问题的原因。

您可以尝试LinearLayout而不是View

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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