[英]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.LayoutParams
的layoutParams
。 在View
中的方法定义中,方法setLayoutParams
接收类型为ViewGroup.LayoutParams
参数,并且ViewGroup
不包含margin属性,因此这可能是问题的原因。
您可以尝试LinearLayout
而不是View
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.