Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom ViewGroup incorrect onLayout
    primarykey
    data
    text
    <p>I'm trying to write my own layout class. But I'm having trouble with the onLayout. If I extend LinearLayout and have it do onLayout and I call super.onMeasure() in my onMeasure, everything works. But it's not working extending ViewGroup. What's showing is the first left most child gets rendered correctly but the rest don't show up or are not the right size.</p> <pre><code>import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; class ManualPanel extends ViewGroup { private int totalDivs; private int[] sizesArr; public ManualPanel(final Context context) { this(context, null); } public ManualPanel(final Context context, final AttributeSet attrs) { super(context, attrs); final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ManualPanel); final String[] sizeStr = a.getString(R.styleable.ManualPanel_sizes).split("/"); final String[] fracArr = sizeStr[0].split(","); a.recycle(); int sum = 0; sizesArr = new int[fracArr.length]; for (int i = 0; i &lt; sizesArr.length; i++) { sizesArr[i] = Integer.valueOf(fracArr[i]); sum += sizesArr[i]; } totalDivs = Math.max(sum, Integer.valueOf(sizeStr[1])); } @Override protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) { final int children = getChildCount(); final int totalWidth = MeasureSpec.getSize(widthMeasureSpec); final int totalHeight = MeasureSpec.getSize(heightMeasureSpec); totalDivs = Math.max(totalDivs, children); final int[] arr = IntArray.copyOf(sizesArr, children); final int width[] = new int[children]; int sum = 0; for (int i = 0; i &lt; children; i++) { width[i] = (arr[i] * totalWidth) / totalDivs; sum += width[i]; } for (int i = 0, diff = totalWidth - sum; diff &gt; 0; i = ++i % children) { width[i]++; diff--; } for (int i = 0; i &lt; children; i++) { final View view = getChildAt(i); final LayoutParams lp = view.getLayoutParams(); lp.width = width[i]; view.setLayoutParams(lp); view.measure(MeasureSpec.EXACTLY | lp.width, MeasureSpec.EXACTLY | totalHeight); // view.measure(MeasureSpec.AT_MOST | lp.width, MeasureSpec.AT_MOST | totalHeight); } setMeasuredDimension(totalWidth, totalHeight); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { final int children = getChildCount(), height = getMeasuredHeight(); for (int i = 0; i &lt; children; i++) { final View view = getChildAt(i); if (view.getVisibility() != View.VISIBLE) continue; final int width = view.getMeasuredWidth(); view.layout(l, 0, left + width, height); l += width; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload