2012-07-13 60 views
0

我有一個frameLayout,在這個佈局中有幾個視圖,我想從頂部到特定距離保留每個視圖。 我曾嘗試下面的代碼,但它似乎不工作視圖Layout_Margin在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); 

細胞View類:

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); 
    } 

} 
+0

在框架佈局中添加視圖不是一個好習慣。您不能,而不是添加相對佈局 – AkashG 2012-07-13 11:57:12

回答

1

您設置爲CellViewView子類的一個layoutParams鍵入LinearLayout.LayoutParams。在View中的方法的definition中,方法setLayoutParams接收ViewGroup.LayoutParams類型的參數並且ViewGroup不包含邊界屬性,因此這可能是問題的原因。

您可以嘗試子類LinearLayout而不是View