Onlayout method on custom layout extending LinearLayout

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)

+4
source share
2 answers

I struggled with this problem for a while. It seems like a lot of time has passed since he was asked, but here is what I did to get it to work. Maybe this will help someone in turn.

The extension LinearLayout means that you do not need to override onLayout if you want it to display your child views in the same way as LinearLayout. All I had to do to get this layout, as expected, was to remove my onLayout method and let this LinearLayout class take care of it.

+1
source

The third and fourth parameters of the layout() method are "Right position relative to the parent" and "Lower position relative to the parent", and not the width and height, as you seem to consider them.

0
source

Source: https://habr.com/ru/post/1493398/


All Articles