Deducing from your comments, you're setting your params when your View
does not have LayoutParams
yet, and they are being overwritten when you attach your View
to the layout. What I would advice you to do is to move the setting of your LayoutParams
to the onAttachedToWindow
method. Then you will be able to obtain LayoutParams
with getLayoutParams()
and modify them.
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();
}
}