【发布时间】:2023-04-05 10:18:01
【问题描述】:
我有一个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);
}
}
【问题讨论】:
-
在框架布局中添加视图不是一个好习惯。你不能,而不是在相对布局中添加
标签: android margin android-framelayout