I have a custom view that extends LinearLayout. This custom view contains several other views, which should be located exactly like LinearLayout, but I couldn’t lay them out correctly ... All subtasks are located on each other, hiding all previously viewed add-ons.
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 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 to set x-position, y-position, width and height of my user view? I set the LayoutParam of my custom view to WRAP_CONTENT, however it still behaves like FILL_PARENT, taking up all the free space available in the parent. It seems that all my efforts to change the position or size do not work at all (I'm even trying to set Padding to try to control the position)