nom*_*nom 5 android android-layoutparams
我用它来以编程方式设置边距,但它不起作用,不应用边距。在构造函数中:
public TimeWindow(Context context, int pixels, int left, int top){
super(context);
ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(pixels, pixels);
params.setMargins(left, top, 0, 0);
this.setLayoutParams(params);
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6430 次 |
| 最近记录: |