Onlayout method on custom layout extending LinearLayout
I have a custom view that is extending LinearLayout. This custom view contains several other views which should layout exactly like LinearLayout, however, I did not managed to lay them out correctly... All the subviews lays on top of each other, hiding all the subviews added previously.
My onLayout and onMeasure as follows:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// Do nothing. Do not call the superclass method--that would start a layout pass
// on this view's children. PieChart lays out its children in onSizeChanged().
super.onLayout(changed, l, t, r, b);
Log.e(LOG_TAG, LOG_TAG + ".onLayout: " + l + ", " + t + ", " + r + ", " + b);
int iChildCount = this.getChildCount();
for ( int i = 0; i < iChildCount; i++ ) {
View pChild = this.getChildAt(i);
pChild.layout(l, t, pChild.getMeasuredWidth(), pChild.getMeasuredHeight());
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Try for a width based on our minimum
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.d(LOG_TAG, LOG_TAG + ".onMeasure: width: " + widthMeasureSpec + " getWidth: " + MeasureSpec.getSize(widthMeasureSpec));
Log.d(LOG_TAG, LOG_TAG + ".onMeasure: height: " + heightMeasureSpec + " getHeight: " + MeasureSpec.getSize(heightMeasureSpec));
Log.d(LOG_TAG, LOG_TAG + ".onMeasure: getPaddingLeft: " + getPaddingLeft() + " getPaddingRight: " + getPaddingRight());
Log.d(LOG_TAG, LOG_TAG + ".onMeasure: getPaddingTop: " + getPaddingTop() + " getPaddingBottom: " + getPaddingBottom());
// http://stackoverflow.com/a/17545273/474330
int iParentWidth = MeasureSpec.getSize(widthMeasureSpec);
int iParentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(iParentWidth, iParentHeight);
int iChildCount = this.getChildCount();
for ( int i = 0; i < iChildCount; i++ ) {
View pChild = this.getChildAt(i);
this.measureChild( pChild,
MeasureSpec.makeMeasureSpec(iParentWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(iParentHeight, MeasureSpec.EXACTLY)
);
}
}
How do I set the x-position, y-position, width and height of my custom view? I have set the LayoutParam of my custom view to WRAP_CONTENT, however, it is still behaving like FILL_PARENT, occupying all the free space available in the parent. It seems like all my effort to change the position or size are not working at all (I've even try to setPadding to try to control the position)
I struggled with this same problem for awhile. It looks like it's been a long time since it was asked but here is what I did to get it to work. Maybe it'll help someone down the line.
Extending LinearLayout means you don't have to override onLayout if you want it to display your child views the same as LinearLayout would. All I had to do to get this to layout as expected was remove my onLayout method and let the LinearLayout class take care of that.
layout()
方法的第3个和第4个参数分别是“Right position,relative to parent”和“Bottom position,relative to parent”,而不是您认为它们的宽度和高度。