我试图在扩展的LinearLayout中调整组件的大小。在方法onLayout()中。
public ExtLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context,attrs,0);
init(attrs);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
Log.d(TAG,
"OnLayout1: Left: " + l + "Right: " + r + "Top: " + t + "Bottom: " + b + " children" + this.getChildCount() + " changed" + changed + " getHeight "
+ this.getMeasuredHeight() + " getWidth " + this.getMeasuredWidth());
Child c = (SeedBar) this.getChildAt(0);
c.setLayoutParams(new LinearLayout.LayoutParams(20,this.getMeasuredHeight()-23));
super.onLayout(changed, l, t, r, b);
}
private void init(AttributeSet attrs) {
this.setOrientation(LinearLayout.VERTICAL);
this.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
this.configureChild();
this.invalidate();
}
private void configureChild() {
Child c = new SeedBar(getContext());
c.setLayoutParams(new LayoutParams(20, 100));
this.addView(c, 0);
}
因此,我将在onLayout()方法中添加一个Child并设置Size,但是什么也不会发生。从视图扩展子级
尽管年龄5岁且未得到解答,但该问题仍然是该问题在Google上排名第一的搜索结果。对于试图解决此自定义组件问题的任何人,都不应在父onLayout()内的子视图上设置LayoutParams,这只是行不通。相反,您想在子视图上调用layout()。
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
int margin = 4
childView.layout(margin, margin, getWidth()-margin, getHeight()-margin)
}