从您的评论中推断,您正在设置您的参数,而您View
还没有设置您的参数,并且当您将您的附加到布局LayoutParams
时它们被覆盖。View
我建议你做的是将你的设置移到LayoutParams
方法onAttachedToWindow
上。然后您将能够获得LayoutParams
并getLayoutParams()
修改它们。
private final int mPixelSize;
private final int mLeftMargin;
private final int mTopMargin;
public TimeWindow(Context context, int pixels, int left, int top){
super(context);
mPixelSize = pixels;
mLeftMargin = left;
mTopMargin = top;
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (getLayoutParams() instanceof MarginLayoutParams){
//if getLayoutParams() returns null, the if condition will be false
MarginLayoutParams layoutParams = (MarginLayoutParams) getLayoutParams();
layoutParams.width = mPixelSize;
layoutParams.height = mPixelSize;
layoutParams.setMargins(mLeftMargin, mTopMargin, 0, 0);
requestLayout();
}
}