【问题标题】:View Layout_Margin doesn't work inside the FrameLayoutView Layout_Margin 在 FrameLayout 中不起作用
【发布时间】: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


【解决方案1】:

您正在设置CellViewView 的子类,layoutParams 类型为LinearLayout.LayoutParams。在View方法的definition中,方法setLayoutParams接收ViewGroup.LayoutParams类型的参数,而ViewGroup不包含margin属性,所以这可能是问题的原因。

您可以尝试继承 LinearLayout 而不是 View

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 2014-10-06
    相关资源
    最近更新 更多